MiniDatabase / buffer.h
buffer.h
Raw
#ifndef BUFFER_H
#define BUFFER_H

#include <vector>
#include <string>
#include <list>
#include "xxhash32.h"

class PageFrame {
public:
    PageFrame(int pageId, char* data) : pageId(pageId), data(data) {}

    int pageId;

    // Variables for two-way linked list, for LRU page replacement algorithm
    int next;
    int previous;

    char* getData() {
        return data;
    }

// private:
    char* data;
};

class ExtendibleHashDirectory {
public:
    ExtendibleHashDirectory(int initialDepth, int maxDepth);

    int addPage(int pageId, PageFrame* pageFrame);

    int getGlobalDepth();

    std::list<PageFrame*>& getPageFrames(int index);  // returns ref to list of pages, CANNOT modify the list thru this

// private:
    int globalDepth; // number of bits in prefix of hash (in other words, current depth)

    int maxDepth;

    int max_chaining;

    std::vector<std::list<PageFrame*>> directoryVec; // a vector of linked-list of PageFrame pointers

    friend class BufferPool;// full access to BufferPool class
};

class BufferPool {
public:
    BufferPool(int initialDepth, int maxDepth);  // size are in units of depths, conversions needs to be done on DB level

    int curSize;
    
    bool no_buffer;

    // For two-way linked list, for LRU page replacement algorithm
    int head;
    int tail;

    ExtendibleHashDirectory directory;

    void insertPage(int pageId, char* data);

    char* getPage(int pageId);

    PageFrame *getPageFrame(int pageId);

    void evictPage(PageFrame *pageFrame);

    void setMaxDirectoryDepth(int depth);

    PageFrame *evictPolicy();

    void emptyBuffer();

    void evictFromSST(int baseline_size, int SST_num);

    void evict_in_range(int start_index, int end_index);

    void destroyBuffer();  // Delete the entire buffer, before DB closes
};
#endif // BUFFER_H