DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • A New Era Has Come, and So Must Your Database Observability
  • Conversational Applications With Large Language Models Understanding the Sequence of User Inputs, Prompts, and Responses
  • Automating Database Operations With Ansible and DbVisualizer
  • Database Monitoring: Key Metrics and Considerations

Trending

  • Implementing Stronger RBAC and Multitenancy in Kubernetes Using Istio
  • Chronicle Services: Low Latency Java Microservices Without Pain
  • Performance Optimization Strategies in Highly Scalable Systems
  • Cognitive AI: The Road To AI That Thinks Like a Human Being
  1. DZone
  2. Data Engineering
  3. Databases
  4. Building Extendible Hash Leaf Page

Building Extendible Hash Leaf Page

In this article, take a look at how to build an extendible hash leaf page.

Oren Eini user avatar by
Oren Eini
·
Updated Dec. 26, 19 · Tutorial
Like (3)
Save
Tweet
Share
17.24K Views

Join the DZone community and get the full member experience.

Join For Free

Oval-shaped green leaf

Building Extendible Hash Leaf Page

An extendible hash is composed of a directory section, which points to leaf pages, and the leaf pages point to where the actual data resides.

My current implementation is incredibly naïve though. The leaf page has an array of records (two int64 values, for key and value) that we scan through to find a match. That works, but it works badly. It means that you have to scan through a lot of records (up to 254, if the value isn't found). At the same time, we also waste quite a bit of space here. We need to store int64 keys and values, but the vast majority of them are going to be much smaller.

Voron uses 8KB pages, with 64 bytes page header, leaving us with 8,128 bytes for actual data. I want to kill two birds in one design decision here. Handling both the lookup costs as well as the space costs. Another factor that I have to keep in mind is that complexity kills.

You might also like:  Real-Time Dashboard With MongoDB

The RavenDB project had a number of dedicated hash tables over the years for specific purposes. Some of them were quite fancy and optimized to the hilt. They were also complex. In a particular case, a particular set of writes and deletes meant that we lost an item in the hash table. It was there, but because we used linear probing on collision, there was an issue with deleting a value under some cases where we would mark the first colliding key as removed but didn't move the second colliding key to its rightful place.

If this makes sense to you, you can appreciate the kind of bug that this caused. It only happened in very particular cases, very hard to track down, and caused nasty issues. If you don't follow the issue description, just assume that it was complex and hard to figure out. I don't like complexity, this is part of why I enjoy extendible hashing so much, it is such a brilliant idea and so simple in concept.

So, whatever method we are talking about, it has to allow fast lookups, be space-efficient, and not be complex.         

The idea is simple, we'll divide the space available in our page to 127 pieces, each with 64 bytes in size. The first byte on each piece will be used to hold the number of entries used in this piece, and the rest of the data will hold varint-encoded pairs of keys and values. It might be easier to understand if we look at the code:      

Java
xxxxxxxxxx
1
35
 
1
typedef struct hash_bucket_piece {
2
    char used_entries;
3
    char data[63];
4
} hash_bucket_piece_t;
5
6
typedef struct hash_bucket {
7
    uint16_t size;
8
    uint16_t capacity;
9
    uint8_t depth;
10
    hash_bucket_piece_t pieces[127];
11
} hash_bucket_t;
12
13
bool hash_table_get(hash_ctx_t* ctx, uint64_t key, uint64_t* value) {
14
    uint32_t bucket_idx = hash_table_bucket_number(ctx, key);
15
    hash_bucket_t* b = ctx->dir->buckets[bucket_idx];
16
    uint32_t piece_idx = (key >> b->depth) % 127;
17
    hash_bucket_piece_t* p = &b->pieces[piece_idx];
18
    uint32_t count = p->used_entries;
19
    char* buf = p->data;
20
    while (count >= 0)
21
    {
22
        uint64_t cur_key = 0;
23
        uint64_t val = 0;
24
        varint_decode(&buf, &cur_key);
25
        varint_decode(&buf, &val);
26
        if (cur_key == key) {
27
            *value = val;
28
            return true;
29
        }
30
31
        count--;
32
    }
33
    
34
    return false;
35
}


I'm showing here the read side of things because that is pretty simple. For writes, you need to do a bit more housekeeping, but not a lot more of it. Some key observations about this piece of code:

  • We need to scan only a single 64 bytes buffer. This fits into a CPU cache line, so it is safe to assume that the cost of actually scanning it for a match is actually much lower than fetching from main memory.
  • We discard all the common prefix of the page, using its depth value. The rest is used as a modulus index directly into the specified location.
  • There is no complexity around linear probing, closed/open addressing, etc. This is because our system can't have collisions.

Actually, the last part is a lie. You are going to get two values that end up in the same piece, obviously. That is a collision, but that requires no other special handling. The fun part here is that when we fill a piece completely, we'll need to split the whole page. That will automatically spread the data across two pages and we get our load factor for free.

Analyzing the cost of lookup in such a scheme, we have:

  • Bit shifts to index into the right bucket (I'm assuming that the directory itself is fully resident in memory).
  • We also need the header of the bucket, so we'll need to read it as well (Here we may have disk read).
  • Modulus constant and then direct addressing to the relevant piece (covered by the previous disk read).
  • Scan 64 bytes to find a particular key using varint.

So in all, we read about 192 bytes (counting values in cache lines) and a single disk read. I expect this to be a pretty efficient result in both time and space.

Further Reading

How Database B-Tree Indexing Works

How We Implement 10x Faster Expression Evaluation With Vectorized Execution

Database

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A New Era Has Come, and So Must Your Database Observability
  • Conversational Applications With Large Language Models Understanding the Sequence of User Inputs, Prompts, and Responses
  • Automating Database Operations With Ansible and DbVisualizer
  • Database Monitoring: Key Metrics and Considerations

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: