LogicalFileSystem.h
1 
10 #ifndef WRENCH_LOGICALFILESYSTEM_H
11 #define WRENCH_LOGICALFILESYSTEM_H
12 
13 #include <string>
14 #include <map>
15 #include <set>
16 
17 #include <wrench/workflow/WorkflowFile.h>
18 
19 namespace wrench {
20 
21  /***********************/
23  /***********************/
24 
25 
30 
31  public:
32 
33  explicit LogicalFileSystem(std::string hostname, std::string ss_name, std::string mount_point);
34 
35  void init();
36 
37  double getTotalCapacity();
38  bool hasEnoughFreeSpace(double bytes);
39  double getFreeSpace();
40  void reserveSpace(WorkflowFile *file, std::string absolute_path);
41  void unreserveSpace(WorkflowFile *file, std::string absolute_path);
42 
43  void createDirectory(std::string absolute_path);
44  bool doesDirectoryExist(std::string absolute_path);
45  bool isDirectoryEmpty(std::string absolute_path);
46  void removeEmptyDirectory(std::string absolute_path);
47  void storeFileInDirectory(WorkflowFile *file, std::string absolute_path);
48  void removeFileFromDirectory(WorkflowFile *file, std::string absolute_path);
49  void removeAllFilesInDirectory(std::string absolute_path);
50  bool isFileInDirectory(WorkflowFile *file, std::string absolute_path);
51  std::set<WorkflowFile *> listFilesInDirectory(std::string absolute_path);
52 
53 
54  private:
55 
56  friend class StorageService;
57 
58  void stageFile(WorkflowFile *file, std::string absolute_path);
59 
60  static std::map<std::string, std::string> mount_points;
61 
62  std::map<std::string, std::set<WorkflowFile*>> content;
63 
64  std::string hostname;
65  std::string ss_name;
66  std::string mount_point;
67  double total_capacity;
68  double occupied_space;
69  std::map<std::string, double> reserved_space;
70 
71  bool initialized;
72 
73  void assertInitHasBeenCalled() {
74  if (not this->initialized) {
75  throw std::runtime_error("LogicalFileSystem::assertInitHasBeenCalled(): A logical file system needs to be initialized before it's been called");
76  }
77  }
78  void assertDirectoryExist(std::string absolute_path) {
79  if (not this->doesDirectoryExist(absolute_path)) {
80  throw std::invalid_argument("LogicalFileSystem::assertDirectoryExists(): directory " + absolute_path + " does not exist");
81  }
82  }
83 
84  void assertDirectoryDoesNotExist(std::string absolute_path) {
85  if (this->doesDirectoryExist(absolute_path)) {
86  throw std::invalid_argument("LogicalFileSystem::assertDirectoryExists(): directory " + absolute_path + " already exists");
87  }
88  }
89 
90  void assertDirectoryIsEmpty(std::string absolute_path) {
91  assertDirectoryExist(absolute_path);
92  if (not this->isDirectoryEmpty(absolute_path)) {
93  throw std::invalid_argument("LogicalFileSystem::assertDirectoryIsEmpty(): directory " + absolute_path + "is not empty");
94  }
95  }
96 
97  void assertFileIsInDirectory(WorkflowFile *file, std::string absolute_path) {
98  assertDirectoryExist(absolute_path);
99  if (this->content[absolute_path].find(file) == this->content[absolute_path].end()) {
100  throw std::invalid_argument("LogicalFileSystem::assertFileIsInDirectory(): File " + file->getID() +
101  " is not in directory " + absolute_path);
102  }
103  }
104 
105  };
106 
107 
108  /***********************/
110  /***********************/
111 
112 }
113 
114 
115 #endif //WRENCH_LOGICALFILESYSTEM_H
void init()
Initializes the Logical File System (must be called before any other operation on this file system)
Definition: LogicalFileSystem.cpp:50
std::set< WorkflowFile * > listFilesInDirectory(std::string absolute_path)
Get the files in a directory as a set.
Definition: LogicalFileSystem.cpp:192
bool isDirectoryEmpty(std::string absolute_path)
Checks whether a directory is empty.
Definition: LogicalFileSystem.cpp:94
double getFreeSpace()
Get the file system's free space.
Definition: LogicalFileSystem.cpp:221
void removeEmptyDirectory(std::string absolute_path)
Remove an empty directory.
Definition: LogicalFileSystem.cpp:107
bool isFileInDirectory(WorkflowFile *file, std::string absolute_path)
Checks whether a file is in a directory.
Definition: LogicalFileSystem.cpp:174
bool doesDirectoryExist(std::string absolute_path)
Checks whether a directory exists.
Definition: LogicalFileSystem.cpp:82
The storage service base class.
Definition: StorageService.h:36
LogicalFileSystem(std::string hostname, std::string ss_name, std::string mount_point)
Constructor.
Definition: LogicalFileSystem.cpp:26
Definition: Alarm.cpp:20
void createDirectory(std::string absolute_path)
Create a new directory.
Definition: LogicalFileSystem.cpp:70
void reserveSpace(WorkflowFile *file, std::string absolute_path)
Reserve space for a file that will be stored.
Definition: LogicalFileSystem.cpp:232
std::string getID()
Get the file id.
Definition: WorkflowFile.cpp:44
void storeFileInDirectory(WorkflowFile *file, std::string absolute_path)
Store file in directory.
Definition: LogicalFileSystem.cpp:122
void unreserveSpace(WorkflowFile *file, std::string absolute_path)
Unreserve space that was saved for a file (likely a failed transfer)
Definition: LogicalFileSystem.cpp:254
A class that implements a weak file system abstraction.
Definition: LogicalFileSystem.h:29
double getTotalCapacity()
Get the total capacity.
Definition: LogicalFileSystem.cpp:202
bool hasEnoughFreeSpace(double bytes)
Checks whether there is enough space to store some number of bytes.
Definition: LogicalFileSystem.cpp:212
void removeFileFromDirectory(WorkflowFile *file, std::string absolute_path)
Remove a file in a directory.
Definition: LogicalFileSystem.cpp:145
A data file used/produced by a WorkflowTask in a Workflow.
Definition: WorkflowFile.h:26
void removeAllFilesInDirectory(std::string absolute_path)
Remove all files in a directory.
Definition: LogicalFileSystem.cpp:159