SwatDB
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 #pragma once
11 
16 // TODO: I don't think we want this here, but it compiles
17 
18 #include <string>
19 #include <vector>
20 #include <mutex>
21 #include "swatdb_types.h"
22 
23 // too many circular includes, just define these types like
24 // this and include the .h file for them in the .cpp files:
25 class File;
26 class Schema;
27 class SwatDB;
28 
29 // TODO: we need to think about what this is and
30 // if this should be a base class with derived classes for
31 // index and relations. For now it is just a struct so that we
32 // can implement the fileIDtoFileName methods
33 // This is here now to support the getFileName method for testing
34 // the diskMgr, but needs more thought
35 // TODO: should this be a private field or do we want to
36 // export it... It might be a protected field?
37 /*
38  * An entry in the catalog. There is one entry for each relation and
39  * index in the system.
40  *
41  * A Catalog Entry includes:
42  * file_id: the entries FileId used as a system-wide identifier for this
43  * relation or index
44  * name: the name of the index or relation
45  * type: the type of entry (Relation or Index)
46  * schema: a pointer to its schema object
47  * file: a pointer to its SwatDB file object (ex. HeapFile, BplusTree)
48  * disk_file_name: the name of the Unix file storing it in diskMgr
49  * valid: set to true if this entry has valid contents
50  * alloc: set to true if this entry has been allocated for use
51  * (it may currently be being filled by another thread)
52  */
53 
58 typedef struct {
59 
65 
69  std::string name;
70 
75 
80 
85 
89  std::string file_name;
90 
94  bool valid;
95 
99  bool alloced; // TODO: only need this if lock on catalog vector
100  // may be released well some of an entry is filled in
101  // (so no other thread will allocate this entry)
102 } CatalogEntry;
103 
134 class Catalog {
135 
136  public:
140  Catalog();
141 
149  Catalog(std::string db_metadata_file);
150 
158  ~Catalog();
159 
160  // TODO: we need to think about if we have one method for
161  // adding both index entries and file entries or different
162  // methods (addRelationEntry and addIndexEntry)
163  // Also, we may want multiple versions with different parameters
164  //
165  // this method(s) may be called from DB loading that reads relation
166  // data from a config file on booting/initing, or it could
167  // be from a "query" layer for creating a new relation or
168  //
169  // TODO: it may make sense for this to be a protected method
170  /*
171  * This method can be called to add an entry for a newly created
172  * index or relation as SwatDB runs, or to add a catalog entry for
173  * an index or relation during that is loaded from saved swatdb state
174  * during SwatDB boot.
175  */
176  //
208  FileId addEntry(std::string name, Schema *schema, File *file, CatType type,
209  std::string file_name);
210 
223  void deleteEntry(FileId file_id);
224 
237  std::string getFileName(FileId file_id);
238 
252  FileId getFileId(std::string name);
253 
266  File* getFile(FileId file_id);
267 
282  Schema *getSchema(FileId file_id);
283 
296  CatType getType(FileId file_id);
297 
304  std::vector<FileId> getFileIds();
305 
306  protected:
307 
326  void _setFile(FileId file_id, File *file_ptr);
327 
334  void _saveDBStateToFile(std::string db_metadata_filename);
335 
336  friend class SwatDB;
337  friend class FileManager;
338 
339  private:
343  std::vector<CatalogEntry> entries;
344 
351 
355  std::mutex cat_mux;
356 };
CatType
Definition: swatdb_types.h:98
std::string name
Definition: catalog.h:69
std::mutex cat_mux
Definition: catalog.h:355
File * file
Definition: catalog.h:84
FileId file_id
Definition: catalog.h:64
Definition: schema.h:27
Definition: filemgr.h:31
FileId next_fid
Definition: catalog.h:350
bool alloced
Definition: catalog.h:99
std::string file_name
Definition: catalog.h:89
Definition: catalog.h:134
Definition: catalog.h:58
CatType entry_type
Definition: catalog.h:74
Schema * schema
Definition: catalog.h:79
bool valid
Definition: catalog.h:94
Definition: swatdb.h:46
std::uint32_t FileId
Definition: swatdb_types.h:29
Definition: file.h:43
std::vector< CatalogEntry > entries
Definition: catalog.h:343