MCPcopy Index your code
hub / github.com/davidmoten/bplustree

github.com/davidmoten/bplustree @0.1.4

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.1.4 ↗ · + Follow
410 symbols 1,523 edges 32 files 15 documented · 4%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

bplustree

Maven Central

codecov

Status: beta

Disk based B+-tree in java using memory mapped files (size limited only by available disk space).

Features

  • size only limited by available disk
  • supports range queries
  • optionally supports duplicate keys
  • much faster read and write than H2 file-based database (because no transactions and different persistence model).

Requirements

  • fast read time for range queries by time and key
  • fast insert time
  • single node implementation (not distributed)
  • use memory-mapped files for speed
  • fixed size keys
  • variable size values
  • very large size storage (>2GB of keys or values)
  • optimized for insert in approximate index order
  • single threaded
  • no transactions
  • delete not supported (?)

Getting started

Add this to your pom.xml:

<dependency>
  <groupId>com.github.davidmoten</groupId>
  <artifactId>bplustree</artifactId>
  <version>VERSION_HERE</version>
</dependency>

Example

Lets create a file based index of timestamped strings (for example lines from a log). Timestamps don't have to be unique.

BPlusTree<Long, String> tree = 
  BPlusTree 
    .file()
    .directory(indexDirectory)
    .maxLeafKeys(32)
    .maxNonLeafKeys(8)
    .segmentSizeMB(1)
    .keySerializer(Serializer.LONG)
    .valueSerializer(Serializer.utf8())
    .naturalOrder();

// insert some values    
tree.insert(1000L, "hello");
tree.insert(2000L, "there");

// search the tree for values with keys between 0 and 3000
// and print out key value pairs
tree.findEntries(0, 3000).forEach(System.out.println);

// search the tree for values with keys between 0 and 3000
// and print out values only
tree.find(0, 3000).forEach(System.out.println);

Duplicate keys

Duplicate keys are allowed by default. You can force overwrite of keyed values by setting .unique(false) in the builder.

Note that for efficiency values with duplicate keys are entered into the tree in reverse insert order so to extract the values retaining insert order a special method is used:

tree.findOrderPreserving(0, 3000);

Using bplustree for String keys

Suppose you want to create a B-+ tree with String keys and those keys can have effectively arbitrary length. Keys are stored as fixed size records (unlike values which can be arbitrary in length). You can use hashes to get good find performance and keep the keys small (4 bytes of hash code) by making a tree of type:

BPlusTree<Integer, StringWithValue> tree = ...

So you insert the String hashcode in the key and combine the String with the value. You find records using the hashcode of the String key and then filter the results based on an exact match of the String component of StringAndValue.

Design

B+-tree index is stored across multiple files (of fixed size). Pointers to values are stored in the tree and the values are stored across a separate set of files (of fixed size).

A LargeByteBuffer abstracts access via Memory Mapped Files to a set of files (ByteBuffer only offers int positions which restricts size to 2GB, LargeByteBuffer offers long positions with no effective limit of size (apart from available disk)).

Extension points exported contracts — how you extend this code

NodeFile (Interface)
(no doc) [6 implementers]
src/main/java/com/github/davidmoten/bplustree/internal/file/NodeFile.java
LargeByteBuffer (Interface)
Similar to ByteBuffer but supports long positions instead of int positions. Does not include tho [2 implementers]
src/main/java/com/github/davidmoten/bplustree/LargeByteBuffer.java
Factory (Interface)
(no doc) [2 implementers]
src/main/java/com/github/davidmoten/bplustree/internal/Factory.java
Serializer (Interface)
(no doc)
src/main/java/com/github/davidmoten/bplustree/Serializer.java
Leaf (Interface)
(no doc)
src/main/java/com/github/davidmoten/bplustree/internal/Leaf.java

Core symbols most depended-on inside this repo

insert
called by 73
src/main/java/com/github/davidmoten/bplustree/internal/Leaf.java
get
called by 51
src/main/java/com/github/davidmoten/bplustree/LargeByteBuffer.java
position
called by 48
src/main/java/com/github/davidmoten/bplustree/internal/file/NodeFile.java
position
called by 35
src/main/java/com/github/davidmoten/bplustree/LargeByteBuffer.java
put
called by 30
src/main/java/com/github/davidmoten/bplustree/LargeByteBuffer.java
keys
called by 30
src/main/java/com/github/davidmoten/bplustree/internal/Node.java
segmentNumber
called by 23
src/main/java/com/github/davidmoten/bplustree/internal/LargeMappedByteBuffer.java
next
called by 19
src/main/java/com/github/davidmoten/bplustree/internal/Leaf.java

Shape

Method 368
Class 34
Interface 8

Languages

Java100%

Modules by API surface

src/main/java/com/github/davidmoten/bplustree/BPlusTree.java53 symbols
src/test/java/com/github/davidmoten/bplustree/BPlusTreeTest.java35 symbols
src/main/java/com/github/davidmoten/bplustree/internal/file/FactoryFile.java35 symbols
src/main/java/com/github/davidmoten/bplustree/internal/LargeMappedByteBuffer.java35 symbols
src/test/java/com/github/davidmoten/bplustree/Benchmarks.java22 symbols
src/main/java/com/github/davidmoten/bplustree/LargeByteBuffer.java21 symbols
src/test/java/com/github/davidmoten/bplustree/LargeByteBufferDelegating.java15 symbols
src/main/java/com/github/davidmoten/bplustree/internal/file/LeafFile.java15 symbols
src/test/java/com/github/davidmoten/bplustree/BPlusTreeFileTest.java14 symbols
src/main/java/com/github/davidmoten/bplustree/internal/memory/LeafMemory.java14 symbols
src/main/java/com/github/davidmoten/bplustree/internal/file/NonLeafFile.java14 symbols
src/test/java/com/github/davidmoten/bplustree/internal/LargeMappedByteBufferTest.java13 symbols

For agents

$ claude mcp add bplustree \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page