GithubHelp home page GithubHelp logo

yaomer / bplus-tree Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 97 KB

A disk-based B+ tree implementation that supports concurrency and transactions

License: BSD 2-Clause "Simplified" License

C++ 99.14% CMake 0.86%
cpp bplus-tree b-tree persistent-storage btree-index db-engine

bplus-tree's Introduction

 _               _ _     
| |__  _ __   __| | |__  
| '_ \| '_ \ / _` | '_ \ 
| |_) | |_) | (_| | |_) |
|_.__/| .__/ \__,_|_.__/ 
      |_|                

Usage

Basic

#include <bpdb/db.h>

#include <iostream>

using namespace std;

int main()
{
    bpdb::DB db(bpdb::options(), "tmpdb");
    db.insert("key", "value");
    auto s = db.insert("key", "value");
    if (s.is_exists()) cout << s.to_str().c_str() << "\n";
    db.update("key", "hello");
    string value;
    s = db.find("key", &value);
    if (s.is_ok()) cout << value.c_str() << "\n";
    db.erase("key");
    s = db.find("key", &value);
    if (s.is_not_found()) cout << s.to_str().c_str() << "\n";
}

Range

void traveldb(bpdb::DB& db)
{
    auto *it = db.new_iterator();
    for (it->seek_to_first(); it->valid(); it->next()) {
        std::cout << "(" << it->key().c_str() << ", " << it->value().c_str() << ")\n";
    }
    delete it;
}

int main()
{
    bpdb::DB db(bpdb::options(), "tmpdb");
    for (int i = 0; i < 100; i++) {
        db.insert("key#" + to_string(i), "value#" + to_string(i));
    }
    traveldb(db);
}

Key Comparator

void traveldb(bpdb::DB& db)
{
    auto *it = db.new_iterator();
    for (it->seek_to_first(); it->valid(); it->next()) {
        std::cout << "(" << it->key().c_str() << ", " << it->value().c_str() << ")\n";
    }
    delete it;
}

bool comparator(const bpdb::key_t& l, const bpdb::key_t& r)
{
   return atoi(strchr(l.c_str(), '#') + 1) < atoi(strchr(r.c_str(), '#') + 1);
}

int main()
{
    bpdb::options ops;
    ops.keycomp = comparator;
    bpdb::DB db(ops, "tmpdb");
    for (int i = 0; i < 100; i++) {
        db.insert("key#" + to_string(i), "value#" + to_string(i));
    }
    traveldb(db);
}

Transaction

int main()
{
    bpdb::DB db(bpdb::options(), "tmpdb");
    auto tx = db.begin();
    tx->insert("key#1", "value#1");
    tx->insert("key#2", "value#2");
    // tx->rollback();
    tx->commit();
    delete tx;
}

bplus-tree's People

Contributors

yaomer avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.