First commit
[anna.git] / example / dbos / workdir / storage / Directory.cpp
1 // ANNA - Anna is Not 'N' 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 #include <anna/core/functions.hpp>
38 #include <anna/core/tracing/Logger.hpp>
39
40 #include <anna/io/functions.hpp>
41
42 #include <anna/dbms/Database.hpp>
43 #include <anna/dbms/Statement.hpp>
44
45 #include <anna/dbos/Repository.hpp>
46
47 #include "../filesystem/Directory.hpp"
48
49 #include "Directory.hpp"
50
51 using namespace std;
52 using namespace anna;
53 using namespace workdir;
54
55 /*static*/
56 storage::Directory::Loader* storage::Directory::st_loader = NULL;
57
58 /*static*/
59 ExclusiveHash <std::string> storage::Directory::st_hash;
60
61 /*static*/
62 int storage::Directory::st_maxSize = 0;
63
64 dbos_prepare_object (storage::Directory);
65
66 void storage::Directory::setup (dbos::Repository& repository, const int maxSize)
67    throw (RuntimeException)
68 {
69    st_loader = new Directory::Loader ();
70    st_maxSize = maxSize;
71
72    Directory::setStorageArea (
73       repository.createStorageArea (                                                  // (1)
74          Directory::getStorageAreaId (), Directory::getStorageAreaName (), Directory::getMaxSize (),
75          Directory::allocator, 2
76       )
77    );
78 }
79
80 storage::Directory* storage::Directory::instantiate (const filesystem::Directory* directory)
81    throw (RuntimeException)
82 {
83    if (st_loader == NULL)
84       throw RuntimeException ("storage::Directory::setup no ha sido invocado", ANNA_FILE_LOCATION);
85
86    Directory* result = NULL;
87
88    try {
89       Guard guard (st_loader, "storage::Directory::Loader");
90       result = dbos::ObjectFacade <Directory>::instance (st_loader->setKey (directory));
91    }
92    catch (dbms::DatabaseException& edb) {
93       throw RuntimeException (edb);
94    }
95
96    return result;
97 }
98
99 void storage::Directory::initialize (dbos::Loader& loader)
100    throw (RuntimeException, dbms::DatabaseException)
101 {
102    Directory::Loader& dbLoader = static_cast <Directory::Loader&> (loader);
103    
104    a_filesystemDirectory = dbLoader.getDirectory ();
105    a_inode = dbLoader.getINode ();
106       
107    LOGINFORMATION (
108       string msg ("storage::Directory::initialize | ");
109       msg += asString ();
110       Logger::information (msg, ANNA_FILE_LOCATION);
111    );
112 }
113
114 void storage::Directory::destroy ()
115    throw ()
116 {
117    LOGINFORMATION (
118       string msg ("storage::Directory::destroy | ");
119       msg += asString ();
120       Logger::information (msg, ANNA_FILE_LOCATION);
121    );
122 }
123
124 string storage::Directory::asString () const
125    throw ()
126 {
127    std::string result ("storage::Directory { Name: ");
128    result += a_filesystemDirectory->getPath ();
129    result += functions::asHexText (" | I-Node: ", a_inode);
130    return result += " }";
131 }
132
133 /* 
134  * Transfiere la información del medio físico al primer nivel de C++.
135  * Posteriormente será interpretada en storage::Directory::initialize
136  */
137 bool storage::Directory::Loader::load (dbms::Connection*, const dbos::StorageArea* ssaa) 
138    throw (RuntimeException)
139 {
140    a_inode = io::functions::getINode (a_filesystemDirectory->getPath ());
141    return true;
142 }
143
144 dbos::Index storage::Directory::Loader::getIndex () const 
145    throw ()
146 {
147    return st_hash.calcule (a_filesystemDirectory->getPath ());   
148 }
149
150 string storage::Directory::Loader::asString () const
151    throw ()
152 {
153    std::string result ("storage::Loader::Directory { Name: ");
154    result += a_filesystemDirectory->getPath ();
155    return result += " }";
156 }
157