MiniDatabase / test-buffer.cpp
test-buffer.cpp
Raw
// A set of tests which test the functionality of the buffer code, independently from the DB.

#include "main.h"
#include "buffer.h"
#include "xxhash32.h"
#include <iostream>
#include <bitset>

void printBuffer(BufferPool* bf) {
    int globalD = bf->directory.getGlobalDepth();
    std::cout << "current global depth " << globalD << "\n";
    for (int i = 0; i < (1 << globalD); i++) {
            std::cout << "directory hash" << i << "\n";
            for (PageFrame* frame : bf->directory.directoryVec[i]){
                std::cout << "   frame id: " << frame->pageId << "\n";
            }
    }
}

int main()
{
    // test was run on a chaining length of 2 (manually changed the max_chaining for simplicity)
    BufferPool* bf = new BufferPool(1, 2);
    int alignment = 4096;
    int size = 4096;

    char* p1 = nullptr;
    posix_memalign(reinterpret_cast<void**>(&p1), alignment, size);

    char* p2 = nullptr;
    posix_memalign(reinterpret_cast<void**>(&p2), alignment, size);

    char* p3 = nullptr;
    posix_memalign(reinterpret_cast<void**>(&p3), alignment, size);

    char* p4 = nullptr;
    posix_memalign(reinterpret_cast<void**>(&p4), alignment, size);

    char* p5 = nullptr;
    posix_memalign(reinterpret_cast<void**>(&p5), alignment, size);

    char* p6 = nullptr;
    posix_memalign(reinterpret_cast<void**>(&p6), alignment, size);

    char* p7 = nullptr;
    posix_memalign(reinterpret_cast<void**>(&p7), alignment, size);

    char* p8 = nullptr;
    posix_memalign(reinterpret_cast<void**>(&p8), alignment, size);

    char* p9 = nullptr;
    posix_memalign(reinterpret_cast<void**>(&p9), alignment, size);

    char* p10 = nullptr;
    posix_memalign(reinterpret_cast<void**>(&p10), alignment, size);


    for (int i=0; i < 4096; i++){
        if (i != 4095){
            p1[i] = 'a';
            p2[i] = 'b';
            p3[i] = 'c';
            p4[i] = 'd';
            p5[i] = 'e';
            p6[i] = 'f';
            p7[i] = 'g';
            p8[i] = 'h';
            p9[i] = 'i';
            p10[i] = 'j';
        }
        else{
            p1[i] = '\0';
            p2[i] = '\0';
            p3[i] = '\0';
            p4[i] = '\0';
            p5[i] = '\0';
            p6[i] = '\0';
            p7[i] = '\0';
            p8[i] = '\0';
            p9[i] = '\0';
            p10[i] = '\0';
        }
    }

    bf->insertPage(1, p1);
    int ih1 = 1;
    uint32_t h1 = XXHash32::hash(&ih1, sizeof(int), 0);
    std::cout << "page 1 inserted with hash: " << std::bitset<32>(h1) << "\n";

    bf->insertPage(2, p2);
    int ih2 = 2;
    uint32_t h2 = XXHash32::hash(&ih2, sizeof(int), 0);
    std::cout << "page 2 inserted with hash: " << std::bitset<32>(h2) << "\n";

    printBuffer(bf);
    // char* res1 = bf->getPage(2);
    // std::cout << res1 << "\n";  // will print out 4096 chars

    bf->insertPage(3, p3);
    int ih3 = 3;
    uint32_t h3 = XXHash32::hash(&ih3, sizeof(int), 0);
    std::cout << "page 3 inserted with hash: " << std::bitset<32>(h3) << "\n";

    printBuffer(bf);

    // char* res3 = bf->getPage(3);
    // std::cout << res3 << "\n";  // will print out 4096 chars

    bf->insertPage(4, p4);
    int ih4 = 4;
    uint32_t h4 = XXHash32::hash(&ih4, sizeof(int), 0);
    std::cout << "page 4 inserted with hash: " << std::bitset<32>(h4) << "\n";

    bf->insertPage(5, p5);
    int ih5 = 5;
    uint32_t h5 = XXHash32::hash(&ih5, sizeof(int), 0);
    std::cout << "page 5 inserted with hash: " << std::bitset<32>(h5) << "\n";

    printBuffer(bf);

    // shrink while items must be evicted to make space
    // bf->setMaxDirectoryDepth(1);
    // printBuffer(bf);

    bf->insertPage(6, p6);
    int ih6 = 6;
    uint32_t h6 = XXHash32::hash(&ih6, sizeof(int), 0);
    std::cout << "page 6 inserted with hash: " << std::bitset<32>(h6) << "\n";

    printBuffer(bf);

    bf->insertPage(7, p7);
    int ih7 = 7;
    uint32_t h7 = XXHash32::hash(&ih7, sizeof(int), 0);
    std::cout << "page 7 inserted with hash: " << std::bitset<32>(h7) << "\n";

    printBuffer(bf);

    char* nothing = bf->getPage(7);  // should be null
    if (nothing == nullptr) {
        std::cout << "Correctly got the nullptr. \n";
    }

    std::cout << "Shrinking to 0. \n";
    bf->setMaxDirectoryDepth(0);

    printBuffer(bf);

    bf->insertPage(8, p8);
    std::cout << "page 8 inserted \n";

    bf->insertPage(9, p9);
    std::cout << "page 9 inserted \n";\

    bf->insertPage(10, p10);
    std::cout << "page 10 inserted \n";

    printBuffer(bf); // not enough space as the max now is 2^1

    bf->destroyBuffer();
}