PLearn 0.1
TemporaryDiskVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // TemporaryDiskVMatrix.cc
00004 //
00005 // Copyright (C) 2005 Olivier Delalleau
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************
00036    * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $
00037    ******************************************************* */
00038 
00039 // Authors: Olivier Delalleau
00040 
00044 #include "TemporaryDiskVMatrix.h"
00045 #include <plearn/io/fileutils.h>    
00046 
00047 namespace PLearn {
00048 using namespace std;
00049 
00050 PLEARN_IMPLEMENT_OBJECT(
00051     TemporaryDiskVMatrix,
00052     "DiskVMatrix to store data temporarily.",
00053     "When a TemporaryDiskVMatrix object is deleted, it automatically removes\n"
00054     "its data directory, as well as its metadata directory.\n"
00055     );
00056 
00058 // TemporaryDiskVMatrix //
00060 TemporaryDiskVMatrix::TemporaryDiskVMatrix()
00061 {}
00062 
00063 TemporaryDiskVMatrix::TemporaryDiskVMatrix(const PPath& filename,
00064                                            bool writable, bool call_build_):
00065     inherited(filename, writable, call_build_)
00066 {
00067     if (call_build_)
00068         build_();
00069 }
00070 
00072 // declareOptions //
00074 void TemporaryDiskVMatrix::declareOptions(OptionList& ol)
00075 {
00076     // Now call the parent class' declareOptions
00077     inherited::declareOptions(ol);
00078 }
00079 
00081 // build //
00083 void TemporaryDiskVMatrix::build()
00084 {
00085     inherited::build();
00086     build_();
00087 }
00088 
00090 // build_ //
00092 void TemporaryDiskVMatrix::build_()
00093 {
00094     last_files_opened.resize(0);
00095     last_dirname = dirname;
00096     addReferenceToFile(dirname);
00097     if (indexf)
00098         last_files_opened.append(dirname / "indexfile");
00099     updateMtime(dirname/"indexfile");
00100     for (int i = 0; i < dataf.length(); i++)
00101         if (dataf[i])
00102             last_files_opened.append(dirname / (tostring(i) + ".data"));
00103     for (int i = 0; i < last_files_opened.length(); i++)
00104         addReferenceToFile(last_files_opened[i]);
00105 }
00106 
00108 // closeCurrentFiles //
00110 void TemporaryDiskVMatrix::closeCurrentFiles()
00111 {
00112     inherited::closeCurrentFiles();
00113     for (int i = 0; i < last_files_opened.length(); i++)
00114         removeReferenceToFile(last_files_opened[i]);
00115     removeReferenceToFile(last_dirname);
00116     last_files_opened.resize(0);
00117 }
00118 
00120 // makeDeepCopyFromShallowCopy //
00122 void TemporaryDiskVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00123 {
00124     deepCopyField(last_files_opened, copies);
00125     // Clearing 'last_files_opened' is important: no file has been opened yet
00126     // by this VMat, as file pointers need to be recreated (this will be done
00127     // in inherited::makeDeepCopyFromShallowCopy(..)).
00128     // Similarly, 'last_dirname' needs to be cleared since it has not actually
00129     // been opened yet by this VMat.
00130     last_files_opened.resize(0);
00131     last_dirname = "";
00132     inherited::makeDeepCopyFromShallowCopy(copies);
00133 }
00134 
00136 // ~TemporaryDiskVMatrix //
00138 TemporaryDiskVMatrix::~TemporaryDiskVMatrix()
00139 {
00140     TVec<PPath> backup_files_opened = last_files_opened.copy();
00141     TemporaryDiskVMatrix::closeCurrentFiles();
00142     bool can_delete_whole_dir = true;
00143     bool one_file_deleted = false;
00144     for (int i = 0; i < backup_files_opened.length(); i++) {
00145         if (noReferenceToFile(backup_files_opened[i])) {
00146             rm(backup_files_opened[i]);
00147             one_file_deleted = true;
00148         } else
00149             can_delete_whole_dir = false;
00150     }
00151     if (one_file_deleted && !can_delete_whole_dir)
00152         PLERROR("In TemporaryDiskVMatrix::~TemporaryDiskVMatrix - Some data "
00153                 "file has been deleted, but not all of them: something must "
00154                 "be wrong");
00155     if (can_delete_whole_dir && !noReferenceToFile(dirname))
00156         PLERROR("In TemporaryDiskVMatrix::~TemporaryDiskVMatrix - Was able to "
00157                 "delete all files in '%s', but it looks like someone is still "
00158                 "using this directory: something must be wrong",
00159                 dirname.absolute().c_str());
00160     if (!can_delete_whole_dir && noReferenceToFile(dirname))
00161         PLERROR("In TemporaryDiskVMatrix::~TemporaryDiskVMatrix - Directory "
00162                 "'%s' is said to have noone referencing it, however it looks "
00163                 "like some files in the directory are still being used: this "
00164                 "should not happen",
00165                 dirname.absolute().c_str());
00166     if (can_delete_whole_dir) {
00167         force_rmdir(last_dirname);
00168         if (hasMetaDataDir()) {
00169             force_rmdir(getMetaDataDir());
00170             // Clear the metadatadir so that the parent class does not try to
00171             // do anything with it.
00172             metadatadir = "";
00173         }
00174     }
00175 }
00176 
00177 } // end of namespace PLearn
00178 
00179 
00180 /*
00181   Local Variables:
00182   mode:c++
00183   c-basic-offset:4
00184   c-file-style:"stroustrup"
00185   c-file-offsets:((innamespace . 0)(inline-open . 0))
00186   indent-tabs-mode:nil
00187   fill-column:79
00188   End:
00189 */
00190 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines