├── release.toml ├── .rustfmt.toml ├── .editorconfig ├── SECURITY.md ├── .gitignore ├── src ├── kv.rs ├── lib.rs ├── err.rs ├── db.rs └── tx.rs ├── Cargo.toml ├── CARGO.md ├── README.md ├── img ├── logo.svg ├── black │ └── logo.svg └── white │ └── logo.svg ├── CODE_OF_CONDUCT.md ├── Cargo.lock └── LICENSE /release.toml: -------------------------------------------------------------------------------- 1 | sign-commit = false 2 | pre-release-commit-message = "Release {{version}}" 3 | tag-message = "" 4 | tag-prefix = "" 5 | consolidate-commits = false 6 | publish = false 7 | push = false 8 | tag = true 9 | -------------------------------------------------------------------------------- /.rustfmt.toml: -------------------------------------------------------------------------------- 1 | blank_lines_lower_bound = 1 2 | blank_lines_upper_bound = 1 3 | closure_block_indent_threshold = 1 4 | edition = "2021" 5 | hard_tabs = true 6 | indent_style = "Block" 7 | match_arm_blocks = true 8 | merge_derives = true 9 | merge_imports = true 10 | reorder_impl_items = true 11 | reorder_imports = true 12 | reorder_modules = true 13 | use_field_init_shorthand = true 14 | use_small_heuristics = "Off" 15 | wrap_comments = true 16 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig coding styles definitions. For more information about the 2 | # properties used in this file, please see the EditorConfig documentation: 3 | # http://editorconfig.org/ 4 | 5 | root = true 6 | 7 | [*] 8 | charset = utf-8 9 | end_of_line = LF 10 | indent_size = 4 11 | indent_style = tab 12 | insert_final_newline = true 13 | trim_trailing_whitespace = true 14 | 15 | [*.{yml,json}] 16 | indent_size = 2 17 | indent_style = space 18 | 19 | [*.{md,diff}] 20 | trim_trailing_whitespace = false 21 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Reporting a Vulnerability 4 | 5 | We take the security of SurrealDB code, software, and cloud platform very 6 | seriously. If you believe you have found a security vulnerability in 7 | SurrealDB, we encourage you to let us know right away. We will investigate 8 | all legitimate reports and do our best to quickly fix the problem. 9 | 10 | Please report any issues or vulnerabilities to security@surrealdb.com, 11 | instead of posting a public issue in GitHub. Please include the version 12 | identifier, and details on how the vulnerability can be exploited. 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ----------------------------------- 2 | # OS X 3 | # ----------------------------------- 4 | 5 | # Directory files 6 | .DS_Store 7 | .AppleDouble 8 | .LSOverride 9 | 10 | # Thumbnail files 11 | ._* 12 | 13 | # Files that might appear on external disk 14 | .Spotlight-V100 15 | .Trashes 16 | 17 | # Directories potentially created on remote AFP share 18 | .AppleDB 19 | .AppleDesktop 20 | Network Trash Folder 21 | Temporary Items 22 | .apdisk 23 | 24 | # ----------------------------------- 25 | # Files 26 | # ----------------------------------- 27 | 28 | **/*.rs.bk 29 | 30 | # ----------------------------------- 31 | # Folders 32 | # ----------------------------------- 33 | 34 | /target/ 35 | -------------------------------------------------------------------------------- /src/kv.rs: -------------------------------------------------------------------------------- 1 | // Copyright © SurrealDB Ltd 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | pub type Key = Vec; 16 | 17 | pub type Val = Vec; 18 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "echodb" 3 | publish = true 4 | edition = "2021" 5 | version = "0.8.0" 6 | readme = "CARGO.md" 7 | authors = ["Tobie Morgan Hitchcock "] 8 | description = "An embedded, in-memory, immutable, copy-on-write database engine" 9 | repository = "https://github.com/surrealdb/echodb" 10 | homepage = "https://github.com/surrealdb/echodb" 11 | keywords = [ 12 | "database", 13 | "embedded-database", 14 | "key-value", 15 | "key-value-store", 16 | "kv-store", 17 | ] 18 | categories = ["database-implementations", "data-structures", "embedded"] 19 | license = "Apache-2.0" 20 | 21 | [dependencies] 22 | arc-swap = "1.7.1" 23 | imbl = "2.0.3" 24 | thiserror = "1.0.61" 25 | tokio = { version = "1.38.0", features = ["sync"] } 26 | 27 | [dev-dependencies] 28 | tokio = { version = "1.38.0", features = ["rt", "macros"] } 29 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright © SurrealDB Ltd 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #![allow(clippy::bool_comparison)] 16 | 17 | mod db; 18 | mod err; 19 | mod tx; 20 | 21 | #[cfg(test)] 22 | pub(crate) mod kv; 23 | 24 | #[doc(inline)] 25 | pub use self::db::*; 26 | #[doc(inline)] 27 | pub use self::err::*; 28 | #[doc(inline)] 29 | pub use self::tx::*; 30 | -------------------------------------------------------------------------------- /src/err.rs: -------------------------------------------------------------------------------- 1 | // Copyright © SurrealDB Ltd 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | //! This module stores the database error types. 16 | 17 | use thiserror::Error; 18 | 19 | /// The errors which can be emitted from a database. 20 | #[derive(Error, Debug)] 21 | pub enum Error { 22 | #[error("Can not open transaction")] 23 | DbError, 24 | 25 | #[error("Transaction is closed")] 26 | TxClosed, 27 | 28 | #[error("Transaction is not writable")] 29 | TxNotWritable, 30 | 31 | #[error("Key being inserted already exists")] 32 | KeyAlreadyExists, 33 | 34 | #[error("Value being checked was not correct")] 35 | ValNotExpectedValue, 36 | } 37 | -------------------------------------------------------------------------------- /CARGO.md: -------------------------------------------------------------------------------- 1 |
2 | 3 |

4 | 5 | EchoDB Logo 6 | 7 |

8 | 9 |

An embedded, in-memory, immutable, copy-on-write, key-value database engine.

10 | 11 |
12 | 13 |

14 | 15 |   16 | 17 |   18 | 19 |   20 | 21 |

22 | 23 | #### Features 24 | 25 | - In-memory database 26 | - Multi-version concurrency control 27 | - Rich transaction support with rollbacks 28 | - Multiple concurrent readers without locking 29 | - Support for serializable, isolated transactions 30 | - Atomicity, Consistency and Isolation from ACID 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 |

4 | 5 | EchoDB Logo 6 | 7 | 8 | EchoDB Logo 9 | 10 |

11 | 12 |

An embedded, in-memory, immutable, copy-on-write, key-value database engine.

13 | 14 |
15 | 16 |

17 | 18 |   19 | 20 |   21 | 22 |   23 | 24 |

25 | 26 | #### Features 27 | 28 | - In-memory database 29 | - Multi-version concurrency control 30 | - Rich transaction support with rollbacks 31 | - Multiple concurrent readers without locking 32 | - Support for serializable, isolated transactions 33 | - Atomicity, Consistency and Isolation from ACID 34 | -------------------------------------------------------------------------------- /img/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 10 | 14 | 16 | 18 | 21 | 23 | 26 | 28 | 29 | -------------------------------------------------------------------------------- /img/black/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 10 | 14 | 16 | 18 | 21 | 23 | 26 | 28 | 29 | -------------------------------------------------------------------------------- /img/white/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 10 | 11 | 12 | 16 | 18 | 20 | 23 | 25 | 28 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behaviour that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behaviour by participants include: 18 | 19 | * The use of sexualised language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behaviour and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behaviour. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviours that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behaviour may be reported by contacting the project team at info@surrealdb.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org), version 1.4, available [here](https://www.contributor-covenant.org/version/1/4/code-of-conduct.html). For answers to common questions about this code of conduct, see [FAQs](https://www.contributor-covenant.org/faq). 44 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "addr2line" 7 | version = "0.22.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" 10 | dependencies = [ 11 | "gimli", 12 | ] 13 | 14 | [[package]] 15 | name = "adler" 16 | version = "1.0.2" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" 19 | 20 | [[package]] 21 | name = "arc-swap" 22 | version = "1.7.1" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" 25 | 26 | [[package]] 27 | name = "backtrace" 28 | version = "0.3.72" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11" 31 | dependencies = [ 32 | "addr2line", 33 | "cc", 34 | "cfg-if", 35 | "libc", 36 | "miniz_oxide", 37 | "object", 38 | "rustc-demangle", 39 | ] 40 | 41 | [[package]] 42 | name = "bitmaps" 43 | version = "3.2.1" 44 | source = "registry+https://github.com/rust-lang/crates.io-index" 45 | checksum = "a1d084b0137aaa901caf9f1e8b21daa6aa24d41cd806e111335541eff9683bd6" 46 | 47 | [[package]] 48 | name = "cc" 49 | version = "1.0.99" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" 52 | 53 | [[package]] 54 | name = "cfg-if" 55 | version = "1.0.0" 56 | source = "registry+https://github.com/rust-lang/crates.io-index" 57 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 58 | 59 | [[package]] 60 | name = "echodb" 61 | version = "0.8.0" 62 | dependencies = [ 63 | "arc-swap", 64 | "imbl", 65 | "thiserror", 66 | "tokio", 67 | ] 68 | 69 | [[package]] 70 | name = "gimli" 71 | version = "0.29.0" 72 | source = "registry+https://github.com/rust-lang/crates.io-index" 73 | checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" 74 | 75 | [[package]] 76 | name = "imbl" 77 | version = "2.0.3" 78 | source = "registry+https://github.com/rust-lang/crates.io-index" 79 | checksum = "978d142c8028edf52095703af2fad11d6f611af1246685725d6b850634647085" 80 | dependencies = [ 81 | "bitmaps", 82 | "imbl-sized-chunks", 83 | "rand_core", 84 | "rand_xoshiro", 85 | "version_check", 86 | ] 87 | 88 | [[package]] 89 | name = "imbl-sized-chunks" 90 | version = "0.1.2" 91 | source = "registry+https://github.com/rust-lang/crates.io-index" 92 | checksum = "144006fb58ed787dcae3f54575ff4349755b00ccc99f4b4873860b654be1ed63" 93 | dependencies = [ 94 | "bitmaps", 95 | ] 96 | 97 | [[package]] 98 | name = "libc" 99 | version = "0.2.155" 100 | source = "registry+https://github.com/rust-lang/crates.io-index" 101 | checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" 102 | 103 | [[package]] 104 | name = "memchr" 105 | version = "2.7.2" 106 | source = "registry+https://github.com/rust-lang/crates.io-index" 107 | checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" 108 | 109 | [[package]] 110 | name = "miniz_oxide" 111 | version = "0.7.3" 112 | source = "registry+https://github.com/rust-lang/crates.io-index" 113 | checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae" 114 | dependencies = [ 115 | "adler", 116 | ] 117 | 118 | [[package]] 119 | name = "object" 120 | version = "0.35.0" 121 | source = "registry+https://github.com/rust-lang/crates.io-index" 122 | checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e" 123 | dependencies = [ 124 | "memchr", 125 | ] 126 | 127 | [[package]] 128 | name = "pin-project-lite" 129 | version = "0.2.14" 130 | source = "registry+https://github.com/rust-lang/crates.io-index" 131 | checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" 132 | 133 | [[package]] 134 | name = "proc-macro2" 135 | version = "1.0.85" 136 | source = "registry+https://github.com/rust-lang/crates.io-index" 137 | checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" 138 | dependencies = [ 139 | "unicode-ident", 140 | ] 141 | 142 | [[package]] 143 | name = "quote" 144 | version = "1.0.36" 145 | source = "registry+https://github.com/rust-lang/crates.io-index" 146 | checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" 147 | dependencies = [ 148 | "proc-macro2", 149 | ] 150 | 151 | [[package]] 152 | name = "rand_core" 153 | version = "0.6.4" 154 | source = "registry+https://github.com/rust-lang/crates.io-index" 155 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 156 | 157 | [[package]] 158 | name = "rand_xoshiro" 159 | version = "0.6.0" 160 | source = "registry+https://github.com/rust-lang/crates.io-index" 161 | checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa" 162 | dependencies = [ 163 | "rand_core", 164 | ] 165 | 166 | [[package]] 167 | name = "rustc-demangle" 168 | version = "0.1.24" 169 | source = "registry+https://github.com/rust-lang/crates.io-index" 170 | checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" 171 | 172 | [[package]] 173 | name = "syn" 174 | version = "2.0.66" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" 177 | dependencies = [ 178 | "proc-macro2", 179 | "quote", 180 | "unicode-ident", 181 | ] 182 | 183 | [[package]] 184 | name = "thiserror" 185 | version = "1.0.61" 186 | source = "registry+https://github.com/rust-lang/crates.io-index" 187 | checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" 188 | dependencies = [ 189 | "thiserror-impl", 190 | ] 191 | 192 | [[package]] 193 | name = "thiserror-impl" 194 | version = "1.0.61" 195 | source = "registry+https://github.com/rust-lang/crates.io-index" 196 | checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" 197 | dependencies = [ 198 | "proc-macro2", 199 | "quote", 200 | "syn", 201 | ] 202 | 203 | [[package]] 204 | name = "tokio" 205 | version = "1.38.0" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" 208 | dependencies = [ 209 | "backtrace", 210 | "pin-project-lite", 211 | "tokio-macros", 212 | ] 213 | 214 | [[package]] 215 | name = "tokio-macros" 216 | version = "2.3.0" 217 | source = "registry+https://github.com/rust-lang/crates.io-index" 218 | checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" 219 | dependencies = [ 220 | "proc-macro2", 221 | "quote", 222 | "syn", 223 | ] 224 | 225 | [[package]] 226 | name = "unicode-ident" 227 | version = "1.0.12" 228 | source = "registry+https://github.com/rust-lang/crates.io-index" 229 | checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" 230 | 231 | [[package]] 232 | name = "version_check" 233 | version = "0.9.4" 234 | source = "registry+https://github.com/rust-lang/crates.io-index" 235 | checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" 236 | -------------------------------------------------------------------------------- /src/db.rs: -------------------------------------------------------------------------------- 1 | // Copyright © SurrealDB Ltd 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | //! This module stores the core in-memory database type. 16 | 17 | use crate::tx::Transaction; 18 | use arc_swap::ArcSwap; 19 | use imbl::OrdMap; 20 | use std::fmt::Debug; 21 | use std::sync::Arc; 22 | use tokio::sync::Mutex; 23 | 24 | /// A transactional in-memory database 25 | #[derive(Clone)] 26 | pub struct Database 27 | where 28 | K: Ord + Clone + Debug + Sync + Send + 'static, 29 | V: Eq + Clone + Debug + Sync + Send + 'static, 30 | { 31 | /// The datastore transaction write lock 32 | pub(crate) writelock: Arc>, 33 | /// The underlying copy-on-write B-tree datastructure 34 | pub(crate) datastore: Arc>>, 35 | } 36 | 37 | /// Create a new transactional in-memory database 38 | pub fn new() -> Database 39 | where 40 | K: Ord + Clone + Debug + Sync + Send + 'static, 41 | V: Eq + Clone + Debug + Sync + Send + 'static, 42 | { 43 | Database { 44 | datastore: Arc::new(ArcSwap::new(Arc::new(OrdMap::new()))), 45 | writelock: Arc::new(Mutex::new(())), 46 | } 47 | } 48 | 49 | impl Database 50 | where 51 | K: Ord + Clone + Debug + Sync + Send + 'static, 52 | V: Eq + Clone + Debug + Sync + Send + 'static, 53 | { 54 | /// Start a new read-only or writeable transaction 55 | pub async fn begin(&self, write: bool) -> Transaction { 56 | match write { 57 | true => { 58 | let lock = Some(self.writelock.clone().lock_owned().await); 59 | Transaction::write(self.clone(), lock) 60 | } 61 | false => { 62 | let lock = None; 63 | Transaction::read(self.clone(), lock) 64 | } 65 | } 66 | } 67 | } 68 | 69 | #[cfg(test)] 70 | mod tests { 71 | 72 | use super::*; 73 | use crate::kv::{Key, Val}; 74 | 75 | #[tokio::test] 76 | async fn begin_tx_readable() { 77 | let db: Database = new(); 78 | db.begin(false).await; 79 | } 80 | 81 | #[tokio::test] 82 | async fn begin_tx_writeable() { 83 | let db: Database = new(); 84 | db.begin(true).await; 85 | } 86 | 87 | #[tokio::test] 88 | async fn writeable_tx_async() { 89 | let db: Database<&str, &str> = new(); 90 | let mut tx = db.begin(true).await; 91 | let res = async { tx.put("test", "something") }.await; 92 | assert!(res.is_ok()); 93 | let res = async { tx.get("test") }.await; 94 | assert!(res.is_ok()); 95 | let res = async { tx.commit() }.await; 96 | assert!(res.is_ok()); 97 | } 98 | 99 | #[tokio::test] 100 | async fn readable_tx_not_writable() { 101 | let db: Database<&str, &str> = new(); 102 | // ---------- 103 | let mut tx = db.begin(false).await; 104 | let res = tx.put("test", "something"); 105 | assert!(res.is_err()); 106 | let res = tx.set("test", "something"); 107 | assert!(res.is_err()); 108 | let res = tx.del("test"); 109 | assert!(res.is_err()); 110 | let res = tx.commit(); 111 | assert!(res.is_err()); 112 | let res = tx.cancel(); 113 | assert!(res.is_ok()); 114 | } 115 | 116 | #[tokio::test] 117 | async fn finished_tx_not_writeable() { 118 | let db: Database<&str, &str> = new(); 119 | // ---------- 120 | let mut tx = db.begin(false).await; 121 | let res = tx.cancel(); 122 | assert!(res.is_ok()); 123 | let res = tx.put("test", "something"); 124 | assert!(res.is_err()); 125 | let res = tx.set("test", "something"); 126 | assert!(res.is_err()); 127 | let res = tx.del("test"); 128 | assert!(res.is_err()); 129 | let res = tx.commit(); 130 | assert!(res.is_err()); 131 | let res = tx.cancel(); 132 | assert!(res.is_err()); 133 | } 134 | 135 | #[tokio::test] 136 | async fn cancelled_tx_is_cancelled() { 137 | let db: Database<&str, &str> = new(); 138 | // ---------- 139 | let mut tx = db.begin(true).await; 140 | tx.put("test", "something").unwrap(); 141 | let res = tx.exists("test").unwrap(); 142 | assert_eq!(res, true); 143 | let res = tx.get("test").unwrap(); 144 | assert_eq!(res, Some("something")); 145 | let res = tx.cancel(); 146 | assert!(res.is_ok()); 147 | // ---------- 148 | let mut tx = db.begin(false).await; 149 | let res = tx.exists("test").unwrap(); 150 | assert_eq!(res, false); 151 | let res = tx.get("test").unwrap(); 152 | assert_eq!(res, None); 153 | let res = tx.cancel(); 154 | assert!(res.is_ok()); 155 | } 156 | 157 | #[tokio::test] 158 | async fn committed_tx_is_committed() { 159 | let db: Database<&str, &str> = new(); 160 | // ---------- 161 | let mut tx = db.begin(true).await; 162 | tx.put("test", "something").unwrap(); 163 | let res = tx.exists("test").unwrap(); 164 | assert_eq!(res, true); 165 | let res = tx.get("test").unwrap(); 166 | assert_eq!(res, Some("something")); 167 | let res = tx.commit(); 168 | assert!(res.is_ok()); 169 | // ---------- 170 | let mut tx = db.begin(false).await; 171 | let res = tx.exists("test").unwrap(); 172 | assert_eq!(res, true); 173 | let res = tx.get("test").unwrap(); 174 | assert_eq!(res, Some("something")); 175 | let res = tx.cancel(); 176 | assert!(res.is_ok()); 177 | } 178 | 179 | #[tokio::test] 180 | async fn multiple_concurrent_readers() { 181 | let db: Database<&str, &str> = new(); 182 | // ---------- 183 | let mut tx = db.begin(true).await; 184 | tx.put("test", "something").unwrap(); 185 | let res = tx.exists("test").unwrap(); 186 | assert_eq!(res, true); 187 | let res = tx.get("test").unwrap(); 188 | assert_eq!(res, Some("something")); 189 | let res = tx.commit(); 190 | assert!(res.is_ok()); 191 | // ---------- 192 | let mut tx1 = db.begin(false).await; 193 | let res = tx1.exists("test").unwrap(); 194 | assert_eq!(res, true); 195 | let res = tx1.exists("temp").unwrap(); 196 | assert_eq!(res, false); 197 | // ---------- 198 | let mut tx2 = db.begin(false).await; 199 | let res = tx2.exists("test").unwrap(); 200 | assert_eq!(res, true); 201 | let res = tx2.exists("temp").unwrap(); 202 | assert_eq!(res, false); 203 | // ---------- 204 | let res = tx1.cancel(); 205 | assert!(res.is_ok()); 206 | let res = tx2.cancel(); 207 | assert!(res.is_ok()); 208 | } 209 | 210 | #[tokio::test] 211 | async fn multiple_concurrent_operators() { 212 | let db: Database<&str, &str> = new(); 213 | // ---------- 214 | let mut tx = db.begin(true).await; 215 | tx.put("test", "something").unwrap(); 216 | let res = tx.exists("test").unwrap(); 217 | assert_eq!(res, true); 218 | let res = tx.get("test").unwrap(); 219 | assert_eq!(res, Some("something")); 220 | let res = tx.commit(); 221 | assert!(res.is_ok()); 222 | // ---------- 223 | let mut tx1 = db.begin(false).await; 224 | let res = tx1.exists("test").unwrap(); 225 | assert_eq!(res, true); 226 | let res = tx1.exists("temp").unwrap(); 227 | assert_eq!(res, false); 228 | // ---------- 229 | let mut txw = db.begin(true).await; 230 | txw.put("temp", "other").unwrap(); 231 | let res = txw.exists("test").unwrap(); 232 | assert_eq!(res, true); 233 | let res = txw.exists("temp").unwrap(); 234 | assert_eq!(res, true); 235 | let res = txw.commit(); 236 | assert!(res.is_ok()); 237 | // ---------- 238 | let mut tx2 = db.begin(false).await; 239 | let res = tx2.exists("test").unwrap(); 240 | assert_eq!(res, true); 241 | let res = tx2.exists("temp").unwrap(); 242 | assert_eq!(res, true); 243 | // ---------- 244 | let res = tx1.exists("temp").unwrap(); 245 | assert_eq!(res, false); 246 | // ---------- 247 | let res = tx1.cancel(); 248 | assert!(res.is_ok()); 249 | let res = tx2.cancel(); 250 | assert!(res.is_ok()); 251 | } 252 | } 253 | -------------------------------------------------------------------------------- /src/tx.rs: -------------------------------------------------------------------------------- 1 | // Copyright © SurrealDB Ltd 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | //! This module stores the database transaction logic. 16 | 17 | use crate::err::Error; 18 | use crate::Database; 19 | use imbl::ordmap::Entry; 20 | use imbl::OrdMap; 21 | use std::borrow::Borrow; 22 | use std::fmt::Debug; 23 | use std::mem::drop; 24 | use std::ops::Range; 25 | use std::sync::Arc; 26 | use tokio::sync::OwnedMutexGuard; 27 | 28 | /// A serializable snapshot isolated database transaction 29 | pub struct Transaction 30 | where 31 | K: Ord + Clone + Debug + Sync + Send + 'static, 32 | V: Eq + Clone + Debug + Sync + Send + 'static, 33 | { 34 | /// Is the transaction complete? 35 | done: bool, 36 | /// Is the transaction writeable? 37 | write: bool, 38 | /// The current snapshot for this transaction 39 | snapshot: OrdMap, 40 | /// The parent database for this transaction 41 | database: Database, 42 | /// The parent datastore transaction write lock 43 | writelock: Option>, 44 | } 45 | 46 | impl Transaction 47 | where 48 | K: Ord + Clone + Debug + Sync + Send + 'static, 49 | V: Eq + Clone + Debug + Sync + Send + 'static, 50 | { 51 | /// Create a new read-only transaction 52 | pub(crate) fn read(db: Database, lock: Option>) -> Transaction { 53 | Transaction { 54 | done: false, 55 | write: false, 56 | snapshot: (*(*db.datastore.load())).clone(), 57 | database: db, 58 | writelock: lock, 59 | } 60 | } 61 | /// Create a new writeable transaction 62 | pub(crate) fn write( 63 | db: Database, 64 | lock: Option>, 65 | ) -> Transaction { 66 | Transaction { 67 | done: false, 68 | write: true, 69 | snapshot: (*(*db.datastore.load())).clone(), 70 | database: db, 71 | writelock: lock, 72 | } 73 | } 74 | 75 | /// Check if the transaction is closed 76 | pub fn closed(&self) -> bool { 77 | self.done 78 | } 79 | 80 | /// Cancel the transaction and rollback any changes 81 | pub fn cancel(&mut self) -> Result<(), Error> { 82 | // Check to see if transaction is closed 83 | if self.done == true { 84 | return Err(Error::TxClosed); 85 | } 86 | // Mark this transaction as done 87 | self.done = true; 88 | // Release the commit lock 89 | if let Some(lock) = self.writelock.take() { 90 | drop(lock); 91 | } 92 | // Continue 93 | Ok(()) 94 | } 95 | 96 | /// Commit the transaction and store all changes 97 | pub fn commit(&mut self) -> Result<(), Error> { 98 | // Check to see if transaction is closed 99 | if self.done == true { 100 | return Err(Error::TxClosed); 101 | } 102 | // Check to see if transaction is writable 103 | if self.write == false { 104 | return Err(Error::TxNotWritable); 105 | } 106 | // Mark this transaction as done 107 | self.done = true; 108 | // Atomically update the datastore using ArcSwap 109 | self.database.datastore.store(Arc::new(self.snapshot.clone())); 110 | // Release the commit lock 111 | if let Some(lock) = self.writelock.take() { 112 | drop(lock); 113 | } 114 | // Continue 115 | Ok(()) 116 | } 117 | 118 | /// Check if a key exists in the database 119 | pub fn exists(&self, key: Q) -> Result 120 | where 121 | Q: Borrow, 122 | { 123 | // Check to see if transaction is closed 124 | if self.done == true { 125 | return Err(Error::TxClosed); 126 | } 127 | // Check the key 128 | let res = self.snapshot.contains_key(key.borrow()); 129 | // Return result 130 | Ok(res) 131 | } 132 | 133 | /// Fetch a key from the database 134 | pub fn get(&self, key: Q) -> Result, Error> 135 | where 136 | Q: Borrow, 137 | { 138 | // Check to see if transaction is closed 139 | if self.done == true { 140 | return Err(Error::TxClosed); 141 | } 142 | // Get the key 143 | let res = self.snapshot.get(key.borrow()).cloned(); 144 | // Return result 145 | Ok(res) 146 | } 147 | 148 | /// Insert or update a key in the database 149 | pub fn set(&mut self, key: Q, val: V) -> Result<(), Error> 150 | where 151 | Q: Into, 152 | { 153 | // Check to see if transaction is closed 154 | if self.done == true { 155 | return Err(Error::TxClosed); 156 | } 157 | // Check to see if transaction is writable 158 | if self.write == false { 159 | return Err(Error::TxNotWritable); 160 | } 161 | // Set the key 162 | self.snapshot.insert(key.into(), val); 163 | // Return result 164 | Ok(()) 165 | } 166 | 167 | /// Insert a key if it doesn't exist in the database 168 | pub fn put(&mut self, key: Q, val: V) -> Result<(), Error> 169 | where 170 | Q: Borrow + Into, 171 | { 172 | // Check to see if transaction is closed 173 | if self.done == true { 174 | return Err(Error::TxClosed); 175 | } 176 | // Check to see if transaction is writable 177 | if self.write == false { 178 | return Err(Error::TxNotWritable); 179 | } 180 | // Set the key 181 | match self.snapshot.entry(key.into()) { 182 | Entry::Vacant(v) => { 183 | v.insert(val); 184 | } 185 | _ => return Err(Error::KeyAlreadyExists), 186 | }; 187 | // Return result 188 | Ok(()) 189 | } 190 | 191 | /// Insert a key if it matches a value 192 | pub fn putc(&mut self, key: Q, val: V, chk: Option) -> Result<(), Error> 193 | where 194 | Q: Borrow + Into, 195 | { 196 | // Check to see if transaction is closed 197 | if self.done == true { 198 | return Err(Error::TxClosed); 199 | } 200 | // Check to see if transaction is writable 201 | if self.write == false { 202 | return Err(Error::TxNotWritable); 203 | } 204 | // Set the key 205 | match (self.snapshot.entry(key.into()), &chk) { 206 | (Entry::Occupied(mut v), Some(w)) if v.get() == w => { 207 | v.insert(val); 208 | } 209 | (Entry::Vacant(v), None) => { 210 | v.insert(val); 211 | } 212 | _ => return Err(Error::ValNotExpectedValue), 213 | }; 214 | // Return result 215 | Ok(()) 216 | } 217 | 218 | /// Delete a key from the database 219 | pub fn del(&mut self, key: Q) -> Result<(), Error> 220 | where 221 | Q: Borrow, 222 | { 223 | // Check to see if transaction is closed 224 | if self.done == true { 225 | return Err(Error::TxClosed); 226 | } 227 | // Check to see if transaction is writable 228 | if self.write == false { 229 | return Err(Error::TxNotWritable); 230 | } 231 | // Remove the key 232 | self.snapshot.remove(key.borrow()); 233 | // Return result 234 | Ok(()) 235 | } 236 | 237 | /// Delete a key if it matches a value 238 | pub fn delc(&mut self, key: Q, chk: Option) -> Result<(), Error> 239 | where 240 | Q: Borrow + Into, 241 | { 242 | // Check to see if transaction is closed 243 | if self.done == true { 244 | return Err(Error::TxClosed); 245 | } 246 | // Check to see if transaction is writable 247 | if self.write == false { 248 | return Err(Error::TxNotWritable); 249 | } 250 | // Remove the key 251 | match (self.snapshot.entry(key.into()), &chk) { 252 | (Entry::Occupied(v), Some(w)) if v.get() == w => { 253 | v.remove(); 254 | } 255 | (Entry::Vacant(_), None) => { 256 | // Nothing to delete 257 | } 258 | _ => return Err(Error::ValNotExpectedValue), 259 | }; 260 | // Return result 261 | Ok(()) 262 | } 263 | 264 | /// Retrieve a range of keys from the databases 265 | pub fn keys(&self, rng: Range, limit: usize) -> Result, Error> 266 | where 267 | Q: Into, 268 | { 269 | // Check to see if transaction is closed 270 | if self.done == true { 271 | return Err(Error::TxClosed); 272 | } 273 | // Compute the range 274 | let beg = rng.start.into(); 275 | let end = rng.end.into(); 276 | // Scan the keys 277 | let res = self.snapshot.range(beg..end).take(limit).map(|(k, _)| k.clone()).collect(); 278 | // Return result 279 | Ok(res) 280 | } 281 | 282 | /// Retrieve a range of key-value pairs from the databases 283 | pub fn scan(&self, rng: Range, limit: usize) -> Result, Error> 284 | where 285 | Q: Into, 286 | { 287 | // Check to see if transaction is closed 288 | if self.done == true { 289 | return Err(Error::TxClosed); 290 | } 291 | // Compute the range 292 | let beg = rng.start.into(); 293 | let end = rng.end.into(); 294 | // Scan the keys 295 | let res = self 296 | .snapshot 297 | .range(beg..end) 298 | .take(limit) 299 | .map(|(k, v)| (k.clone(), v.clone())) 300 | .collect(); 301 | // Return result 302 | Ok(res) 303 | } 304 | } 305 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright © SurrealDB Ltd 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------