Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / example / dbos / workdir / storage / File.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <anna/core/functions.hpp>
10 #include <anna/core/tracing/Logger.hpp>
11
12 #include <anna/io/functions.hpp>
13
14 #include <anna/dbms/Database.hpp>
15 #include <anna/dbms/Statement.hpp>
16
17 #include <anna/dbos/Repository.hpp>
18
19 #include "../filesystem/File.hpp"
20 #include "../filesystem/Directory.hpp"
21
22 #include "File.hpp"
23 #include "Directory.hpp"
24
25 using namespace std;
26 using namespace anna;
27 using namespace workdir;
28
29 /*static*/
30 storage::File::Loader* storage::File::st_loader = NULL;
31
32 /*static*/
33 ExclusiveHash <std::string> storage::File::st_hash;
34
35 /*static*/
36 int storage::File::st_maxSize = 0;
37
38 dbos_prepare_object (storage::File);
39
40 void storage::File::setup (dbos::Repository& repository, const int maxSize)
41    throw (RuntimeException)
42 {
43    st_loader = new File::Loader ();
44    st_maxSize = maxSize;
45
46    File::setStorageArea (
47       repository.createStorageArea (                                                  // (1)
48          File::getStorageAreaId (), File::getStorageAreaName (), File::getMaxSize (),
49          File::allocator, 2
50       )
51    );
52 }
53
54 storage::File* storage::File::instantiate (const filesystem::File* file)
55    throw (RuntimeException)
56 {
57    if (st_loader == NULL)
58       throw RuntimeException ("storage::File::setup no ha sido invocado", ANNA_FILE_LOCATION);
59
60    File* result = NULL;
61
62    try {
63       Guard guard (st_loader, "storage::File::Loader");
64       result = dbos::ObjectFacade <File>::instance (st_loader->setKey (file));
65    }
66    catch (dbms::DatabaseException& edb) {
67       throw RuntimeException (edb);
68    }
69
70    return result;
71 }
72
73 void storage::File::initialize (dbos::Loader& loader)
74    throw (RuntimeException, dbms::DatabaseException)
75 {
76    File::Loader& dbLoader = static_cast <File::Loader&> (loader);
77    
78    a_filesystemFile = dbLoader.getFile ();
79    a_parent = Directory::instantiate (filesystem::Directory::down_cast (a_filesystemFile->getParent ()));
80    a_inode = dbLoader.getINode ();
81       
82    LOGINFORMATION (
83       string msg ("storage::File::initialize | ");
84       msg += asString ();
85       Logger::information (msg, ANNA_FILE_LOCATION);
86    );
87 }
88
89 void storage::File::destroy ()
90    throw ()
91 {   
92    LOGINFORMATION (
93       string msg ("storage::File::destroy | ");
94       msg += asString ();
95       Logger::information (msg, ANNA_FILE_LOCATION);
96    );
97    Directory::release (a_parent);   
98 }
99
100 string storage::File::asString () const
101    throw ()
102 {
103    std::string result ("storage::File { Name: ");
104    result += a_filesystemFile->getPath ();
105    result += functions::asHexText (" | I-Node: ", a_inode);
106    return result += " }";
107 }
108
109 /* 
110  * Transfiere la información del medio físico al primer nivel de C++.
111  * Posteriormente será interpretada en storage::File::initialize
112  */
113 bool storage::File::Loader::load (dbms::Connection*, const dbos::StorageArea* ssaa) 
114    throw (RuntimeException)
115 {
116    a_inode = io::functions::getINode (a_filesystemFile->getPath ());
117    return true;
118 }
119
120 dbos::Index storage::File::Loader::getIndex () const 
121    throw ()
122 {
123    return st_hash.calcule (a_filesystemFile->getPath ());   
124 }
125
126 string storage::File::Loader::asString () const
127    throw ()
128 {
129    std::string result ("storage::Loader::File { Name: ");
130    result += a_filesystemFile->getPath ();
131    return result += " }";
132 }
133