Skip to content

Commit efcf873

Browse files
first commit
0 parents  commit efcf873

12 files changed

+293
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# CZ4031_BPTree_Project

Summarised_Project_Brief.pdf

61.1 KB
Binary file not shown.

src/block.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <iostream>
2+
3+
#include "block.h"
4+
5+
using namespace std;
6+
7+
void Block::AddRecord(Record record){
8+
__records.push_back(record);
9+
}
10+
11+
void Block::DeleteRecord(float numVotes){
12+
if(__records.size() == 0){
13+
cerr << "[ERROR] Unable to delete record, no records avaliable" << endl;
14+
return;
15+
}
16+
17+
cout << "Deleting......." << endl;
18+
for(int i = 0; i < __records.size(); i++){
19+
if(__records[i].getNumVotes() == numVotes){
20+
cout << __records[i].getMovieName() + ": " + to_string(__records[i].getNumVotes()) + "," << endl;
21+
__records.erase(__records.begin() + i);
22+
}
23+
}
24+
}
25+
26+
vector<Record> Block::FindRecords(float numVotes){
27+
vector<Record> foundRecords;
28+
for (Record r : __records) {
29+
if (r.getNumVotes() == numVotes) {
30+
foundRecords.push_back(r);
31+
}
32+
}
33+
// return empty record, but should not happen
34+
return foundRecords;
35+
}
36+
37+
int Block::getNumRecord(){
38+
return __records.size();
39+
}
40+
41+
int Block::getBlockSize(){
42+
return __blockSize;
43+
}
44+
45+
void Block::ShowRecordsInBlock(){
46+
for (Record r : __records) {
47+
cout << r.getMovieName() << " ,";
48+
}
49+
cout << endl;
50+
}
51+
52+
bool Block::checkSpace() {
53+
return __records.size() < __blockSize;
54+
}

src/block.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef H_BLOCK
2+
#define H_BLOCK
3+
4+
#include <vector>
5+
#include <memory>
6+
7+
#include "record.h"
8+
9+
using namespace std;
10+
11+
struct Block {
12+
private:
13+
int __blockSize;
14+
15+
public:
16+
vector<Record> __records;
17+
18+
Block(int blockSize){
19+
blockSize = __blockSize;
20+
}
21+
22+
void AddRecord(Record record);
23+
24+
void DeleteRecord(float numVotes);
25+
26+
// Getters
27+
int getNumRecord();
28+
int getBlockSize();
29+
30+
vector<Record> FindRecords(float numVotes);
31+
32+
void ShowRecordsInBlock();
33+
34+
bool checkSpace();
35+
36+
~Block() = default;
37+
};
38+
39+
#endif

src/bpTree.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
3+
#include "bpTree.h"
4+
using namespace std;
5+
6+
void BPTree::Insert(float numVotes, Block block){
7+
8+
}
9+
10+
void BPTree::Delete(float numVotes){
11+
12+
}
13+
14+

src/bpTree.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef BPLUSTREE_H
2+
#define BPLUSTREE_H
3+
4+
#include <memory>
5+
#include <vector>
6+
#include <utility>
7+
8+
#include "treenode.h"
9+
#include "block.h"
10+
11+
using namespace std;
12+
class BPTree{
13+
shared_ptr<Node> root;
14+
int __treesize; // max number of keys in a tree node
15+
int numNodes; // number of nodes in this tree
16+
17+
public:
18+
BPTree(int size){
19+
__treesize = size;
20+
root = nullptr;
21+
numNodes = 0;
22+
}
23+
24+
~BPTree() = default;
25+
void Insert(float numVotes, Block block);
26+
void Delete(float numVotes);
27+
};
28+
29+
#endif // BPLUSTREE_H

src/record.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "record.h"
2+
3+
using namespace std;
4+
5+
int Record::getRecordSize(){
6+
return sizeof(__movieName) + sizeof(__avgRating) + sizeof(__numVotes);
7+
}
8+
9+
string Record::RecordStringFormat(){
10+
return __movieName + ":" + to_string(__avgRating) + " | " + to_string(__numVotes) + "\n";
11+
}
12+
13+
string Record::getMovieName(){
14+
return __movieName;
15+
}
16+
17+
float Record::getAverageRating(){
18+
return __avgRating;
19+
}
20+
21+
int Record::getNumVotes(){
22+
return __numVotes;
23+
}
24+
25+
void Record::setRecord(string movieName, float averageRating, int numVotes){
26+
__movieName = movieName;
27+
__avgRating = averageRating;
28+
__numVotes = numVotes;
29+
}

src/record.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef H_RECORD
2+
#define H_RECORD
3+
4+
#include <string>
5+
using namespace std;
6+
/* Representing a record in the data base */
7+
struct Record{
8+
private:
9+
string __movieName;
10+
float __avgRating;
11+
int __numVotes;
12+
13+
public:
14+
Record(string movieName, float averageRating, int numVotes){
15+
__movieName = movieName;
16+
__avgRating = averageRating;
17+
__numVotes = numVotes;
18+
}
19+
20+
// Getters
21+
int getRecordSize();
22+
string getMovieName();
23+
float getAverageRating();
24+
int getNumVotes();
25+
26+
// Setter
27+
void setRecord(string movieName, float averageRating, int numVotes);
28+
29+
string RecordStringFormat();
30+
31+
~Record() = default;
32+
};
33+
34+
#endif

src/storage.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
#include <iostream>
3+
#include <vector>
4+
5+
#include "storage.h"
6+
7+
using namespace std;
8+
void Storage::AddBlock(Block block) {
9+
__blocks.push_back(block);
10+
}
11+
12+
int Storage::getNumBlocks() {
13+
return __blocks.size();
14+
}
15+
16+
int Storage::getNumRecords() {
17+
int numRecords = 0;
18+
for(Block b : __blocks){
19+
numRecords += b.getNumRecord();
20+
}
21+
return numRecords;
22+
}
23+
24+
int Storage::getStorageSize() {
25+
int storageSize = 0;
26+
for(Block b : __blocks){
27+
storageSize += b.getBlockSize();
28+
}
29+
return storageSize;
30+
}
31+
32+
33+
34+
35+
36+

src/storage.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef H_STORAGE
2+
#define H_STORAGE
3+
4+
#include <iostream>
5+
#include <vector>
6+
7+
#include "block.h"
8+
9+
using namespace std;
10+
struct Storage {
11+
public:
12+
vector<Block> __blocks; // Shared pointer used to automatically destroy block when it is empty
13+
void AddBlock(Block block);
14+
15+
// Getters
16+
int getNumBlocks();
17+
int getNumRecords();
18+
int getStorageSize();
19+
20+
~Storage() = default;
21+
};
22+
23+
#endif

src/treenode.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <memory>
2+
#include <vector>
3+
#include <utility>
4+
5+
#include "treenode.h"
6+
using namespace std;
7+
bool Node::getIsLeaf(){
8+
return __isLeaf;
9+
}
10+
11+
int Node::getSize(){
12+
return __size;
13+
}

src/treenode.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <memory>
2+
#include <vector>
3+
#include <utility>
4+
using namespace std;
5+
6+
struct Node {
7+
private:
8+
bool __isLeaf;
9+
int __size; // max number of keys in the Node
10+
public:
11+
Node(bool isLeaf, int size){
12+
__isLeaf = isLeaf;
13+
__size = size;
14+
}
15+
//Getters
16+
bool getIsLeaf();
17+
int getSize();
18+
19+
vector<shared_ptr<void>> ptrs; // non-leaf node points to node, leaf node points to vector with shared_ptr of the blocks
20+
vector<float> keys;
21+
};

0 commit comments

Comments
 (0)