SwatDB
Loading...
Searching...
No Matches
catalog.h
Go to the documentation of this file.
1/*
2 * SwatDB
3 *
4 * @authors: See Contributors.doc for code contributors
5 *
6 * Copyright (c) 2020 Swarthmore College Computer Science Department
7 * Swarthmore PA, Professors Tia Newhall, Ameet Soni
8 */
9
10#ifndef _SWATDB_CATALOG_H_
11#define _SWATDB_CATALOG_H_
12
17// TODO: I don't think we want this here, but it compiles
18
19#include <string>
20#include <vector>
21#include <mutex>
22#include <unordered_map>
23#include "swatdb_types.h"
24
25// too many circular includes, just define these types like
26// this and include the .h file for them in the .cpp files:
27class File;
28class Schema;
29class SwatDB;
30class SearchKeyFormat;
31
32// TODO: we need to think about what this is and
33// if this should be a base class with derived classes for
34// index and relations. For now it is just a struct so that we
35// can implement the fileIDtoFileName methods
36// This is here now to support the getFileName method for testing
37// the diskMgr, but needs more thought
38// TODO: should this be a private field or do we want to
39// export it... It might be a protected field?
40/*
41 * An entry in the catalog. There is one entry for each relation and
42 * index in the system.
43 *
44 * A Catalog Entry includes:
45 * file_id: the entries FileId used as a system-wide identifier for this
46 * relation or index
47 * name: the name of the index or relation
48 * type: the type of entry (Relation or Index)
49 * schema: a pointer to its schema object
50 * file: a pointer to its SwatDB file object (ex. HeapFile, BplusTree)
51 * disk_file_name: the name of the Unix file storing it in diskMgr
52 * valid: set to true if this entry has valid contents
53 * alloc: set to true if this entry has been allocated for use
54 * (it may currently be being filled by another thread)
55 */
56
61typedef struct {
62
68
72 std::string name;
73
78
83
88
93
97 std::string file_name;
98
102 bool valid;
103
107 bool alloced; // TODO: only need this if lock on catalog vector
108 // may be released well some of an entry is filled in
109 // (so no other thread will allocate this entry)
110
116
121 std::vector<FileId> index_list;
122
128
130
161class Catalog {
162
163 public:
168
176 Catalog(std::string db_metadata_file);
177
186
187 // TODO: we need to think about if we have one method for
188 // adding both index entries and file entries or different
189 // methods (addRelationEntry and addIndexEntry)
190 // Also, we may want multiple versions with different parameters
191 //
192 // this method(s) may be called from DB loading that reads relation
193 // data from a config file on booting/initing, or it could
194 // be from a "query" layer for creating a new relation or
195 //
196 // TODO: it may make sense for this to be a protected method
197 /*
198 * This method can be called to add an entry for a newly created
199 * index or relation as SwatDB runs, or to add a catalog entry for
200 * an index or relation during that is loaded from saved swatdb state
201 * during SwatDB boot.
202 */
203 //
238 FileId addEntry(std::string name, Schema *schema, File *file,
239 SearchKeyFormat *key_format, CatType type, FileId relation,
240 std::string file_name, bool to_save = true);
241
254 void deleteEntry(FileId file_id);
255
262
266 std::uint32_t getNumValid();
267
280 std::string getFileName(FileId file_id);
281
296 FileId getFileId(std::string name);
297
309
322 File* getFile(FileId file_id);
323
339
353
360 std::vector<FileId> getFileIds();
361
370 void setSave(bool save_on_exit, std::string rel_name);
371
380 void setRelName(std::string old_rel_name, std::string new_rel_name);
381
382 protected:
383
402 void _setFile(FileId file_id, File *file_ptr);
403
410 void _saveDBStateToFile(std::string db_metadata_filename);
411
412 friend class SwatDB;
413 friend class FileManager;
414
415 private:
416
417 std::unordered_map<std::string, FileId> relations;
421 std::vector<CatalogEntry> entries;
422
432
440 std::uint32_t num_valid_entries;
441
445 std::mutex cat_mux;
446};
447
448#endif
Definition catalog.h:161
FileId next_fid
Definition catalog.h:431
Catalog(std::string db_metadata_file)
create a catalog from existing DB state stored in meta data file
FileId getFileId(std::string name)
Returns the FileId associated with the File identified by the given relation name.
File * getFile(FileId file_id)
Returns the File object associated with the given FileId.
std::uint32_t getNumValid()
Returns the number of valid entries in the catalog.
void setRelName(std::string old_rel_name, std::string new_rel_name)
Setter method for the relation name and file name of a relation. Note that both the rel_name and the ...
std::string getFileName(FileId file_id)
Returns the filename associated with the given FileId.
void _setFile(FileId file_id, File *file_ptr)
Set the File * field of a Catalog entry.
std::vector< CatalogEntry > entries
Definition catalog.h:421
void _saveDBStateToFile(std::string db_metadata_filename)
Valled by SwatDB on shutdown to save DB meta data state to a file.
std::mutex cat_mux
Definition catalog.h:445
Schema * getSchema(FileId file_id)
Returns the schema of the requested file.
void deleteEntry(FileId file_id)
Deletes an entry from the database.
CatType getType(FileId file_id)
Returns the type of the requested file.
std::uint32_t num_valid_entries
Definition catalog.h:440
Catalog()
creates an empty Catalog (should only be one in the system)
void printCatInfo()
prints out all files in the catalog and its type, then prints out the relation file associated with i...
std::vector< FileId > getFileIds()
Gets the set of valid FileIDs in the system.
FileId addEntry(std::string name, Schema *schema, File *file, SearchKeyFormat *key_format, CatType type, FileId relation, std::string file_name, bool to_save=true)
Adds an entry to the catalog with a defined schema. May be called to create an entry for an index or ...
FileId getRelationFileId(FileId file_id)
Returns the FileId for the relation file associated with the given index file id.
void setSave(bool save_on_exit, std::string rel_name)
Setter method for the boolean save_on_exit.
~Catalog()
Destroys the Catalog object.
Definition filemgr.h:32
Definition file.h:45
Definition schema.h:37
Definition searchkeyformat.h:32
Definition swatdb.h:48
Definition catalog.h:61
std::string name
Definition catalog.h:72
std::vector< FileId > index_list
Definition catalog.h:121
bool save_on_exit
Definition catalog.h:127
File * file
Definition catalog.h:92
bool valid
Definition catalog.h:102
SearchKeyFormat * key_format
Definition catalog.h:87
FileId relation
Definition catalog.h:115
FileId file_id
Definition catalog.h:67
bool alloced
Definition catalog.h:107
CatType entry_type
Definition catalog.h:77
std::string file_name
Definition catalog.h:97
Schema * schema
Definition catalog.h:82
std::uint32_t FileId
Definition swatdb_types.h:33
CatType
Definition swatdb_types.h:145