Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/edadma/b-tree
B+ Tree implementation in Scala
https://github.com/edadma/b-tree
Last synced: about 2 months ago
JSON representation
B+ Tree implementation in Scala
- Host: GitHub
- URL: https://github.com/edadma/b-tree
- Owner: edadma
- License: isc
- Created: 2015-07-11T20:54:25.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-12-17T14:36:59.000Z (about 3 years ago)
- Last Synced: 2023-09-11T05:50:26.236Z (over 1 year ago)
- Language: Scala
- Size: 1.24 MB
- Stars: 13
- Watchers: 5
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
b-tree
======*b-tree* is a B+ Tree implementation in Scala. *b-tree* is designed to be both generic (type parameters for keys and values, and an abstract type for node references) and general (doesn't care how the tree is stored). An extending class needs to implement a number of simple methods and node type that provide storage independence.
Scaladoc library documentation can be found at http://edadma.github.io/b-tree.
As an example, the following code
import xyz.hyperreal.btree.{MemoryBPlusTree, FileBPlusTree}
object Example1 extends App {
val memoryTree = new MemoryBPlusTree[String, Any]( 5 )
memoryTree.insertKeys( "k", "z", "p", "d", "b", "v", "h", "x", "o", "y", "c", "t", "j", "n", "f", "l", "s", "q", "i", "m", "e", "u", "w", "a", "g", "r" )
memoryTree.diagram( "memoryTree" )
val fileTree = new FileBPlusTree[String, Any]( "btree", 5 )
fileTree.insertKeys( "k", "z", "p", "d", "b", "v", "h", "x", "o", "y", "c", "t", "j", "n", "f", "l", "s", "q", "i", "m", "e", "u", "w", "a", "g", "r" )
fileTree.diagram( "fileTree" )
fileTree.close
println( memoryTree.boundedKeysIterator(('>, "c"), ('<, "l")).mkString(", ") )
println( memoryTree.reverseKeysIterator.mkString(", ") )
}
produces identical B+ Tree structures in memory and on disk, and therefore the generated image files `memoryTree` and `fileTree` will be identical. The diagram that is produced is![tree](tree1.png)
The call to `boundedKeysIterator` above produces an iterator over the keys
d, e, f, g, h, i, j, k
since these are the ones that are strictly between `c` and `l`. The call to `reverseKeysIterator` produces an iterator over the keysz, y, x, w, v, u, t, s, r, q, p, o, n, m, l, k, j, i, h, g, f, e, d, c, b, a
Adding *b-tree* to a project
----------------------------There are two ways to add *b-tree* to a project. If you just want either an in-memory B+ tree "map" or file based B+ tree persistant storage for your project, then the concrete classes `MemoryBPlusTree` and `FileBPlusTree` work out-of-the-box. On the other hand, if you want to integrate the B+ tree algorithms into your project, then you can extend the abstract class `BPlusTree` by implementing it's storage abstraction methods to hook it into your project.
### Extending `BPlusTree`
*b-tree* has one abstract class called `BPlusTree` which provides for interaction with a B+ tree. The concrete methods operate through a storage abstraction layer composed of 1 abstract type (the node type), 4 variables, and 34 methods. To use *b-tree* in a project, it is necessary to extend `BPlusTree` and provide implementations for all the abstract members. 34 methods sounds like a lot but they are all easy to implement. A lot of effort was put into coding `BPlusTree` so that it's abstraction layer would be dead simple to implement.
There are two example implementations provided: `MemoryBPlusTree` and `FileBPlusTree`. `MemoryBPlusTree` is just a normal in-memory data structure where the node type is a simple class. `FileBPlusTree` provides for persistant storage of key/value pairs in disk files. To see how easy it can be to implement the abstraction layer just look at the source code for `MemoryBPlusTree`.
To begin working on adding *b-tree* to a project, visit the [scaladoc library documentation](http://edadma.github.io/b-tree). Next, click on `BPlusTree` and then select visibility "All" to see all the protected members. Everything under "Type Members" and "Abstract Value Members" must be implemented. All the information needed is in the member descriptions and in the class description at the top.
License
-------*b-tree* is distributed under the MIT License, meaning that you are free to use it in your free or proprietary software.
Usage
-----Use the following elements to use *b-tree* in your Maven project:
hyperreal
https://dl.bintray.com/edadma/maven
xyz.hyperreal
b-tree
0.5
Add the following to your `build.sbt` file to use *b-tree* in your SBT project:
resolvers += "Hyperreal Repository" at "https://dl.bintray.com/edadma/maven"
libraryDependencies += "xyz.hyperreal" %% "b-tree" % "0.5"
## Building
### Requirements
- SBT 1.2+
- Java 8+
- Scala 2.12+
- graphviz, imageMagick (optionally, for tree diagrams)### Clone
git clone git://github.com/edadma/b-tree.git
### Compile and TestFor compiling and testing, it's best to use the following command:
testOnly -- -l slow
to exclude the "stress tests" that are tagged as "slow". Some stress testing is still done with this command. If you just type `test`, you may have to wait many minutes for all the tests to complete. The example above can then be executed withtest:runMain Example1