Updated license
[anna.git] / example / dbos / workdir / storage / File.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef dbos_workdir_storage_File_hpp
38 #define dbos_workdir_storage_File_hpp
39
40 #include <anna/dbos/Object.hpp>
41 #include <anna/dbos/ObjectFacade.hpp>
42 #include <anna/dbos/Loader.hpp>
43
44 namespace anna {
45 namespace dbms {
46 class Database;
47 class Connection;
48 }
49 namespace dbos {
50 class Repository;
51 }
52 }
53
54 namespace workdir {
55
56 namespace filesystem {
57 class File;
58 }
59
60 namespace storage {
61
62 class Directory;
63
64 using namespace anna;
65
66 class File : public dbos::Object, public dbos::ObjectFacade <File> {
67 public:
68   const Directory* getParent() const throw() { return a_parent; }
69   const filesystem::File* getFilesystemFile() const throw() { return a_filesystemFile; }
70   int getINode() const throw() { return a_inode; }
71
72   std::string asString() const throw();
73
74   static void setup(dbos::Repository&, const int maxSize) throw(RuntimeException);
75   static File* instantiate(const filesystem::File*) throw(RuntimeException);
76
77   static const char* getStorageAreaName() throw() { return "storage::File"; }
78   static const dbos::Size getMaxSize() throw() { return st_maxSize; }
79
80 private:
81   class Loader : public dbos::Loader {
82   public:
83     Loader() : dbos::Loader() {;}
84
85     Loader& setKey(const filesystem::File* file) throw() {
86       a_filesystemFile = file;
87       return *this;
88     }
89
90     const filesystem::File* getFile() const throw() { return a_filesystemFile; }
91     int getINode() const throw() { return a_inode; }
92
93     dbos::Index getIndex() const throw();
94     std::string asString() const throw();
95
96   private:
97     const filesystem::File* a_filesystemFile;
98     int a_inode;
99
100     // dbms::Statement is not required
101     dbms::Statement* initialize(dbms::Database&) throw(RuntimeException) { return NULL; }
102     bool load(dbms::Connection*, const dbos::StorageArea*) throw(RuntimeException);
103   };
104
105   Directory* a_parent;
106   const filesystem::File* a_filesystemFile;
107   int a_inode;
108
109   static Loader* st_loader;
110   static ExclusiveHash <std::string> st_hash;
111   static int st_maxSize;
112
113   File() : a_parent(NULL) { ; }
114   File(const File&);
115
116   void initialize(dbos::Loader& loader) throw(RuntimeException, dbms::DatabaseException);
117   void destroy() throw();
118
119   dbos_declare_object(File);
120 };
121
122 }
123 }
124
125 #endif