LogCabin
Storage/Layout.h
Go to the documentation of this file.
00001 /* Copyright (c) 2015 Diego Ongaro
00002  *
00003  * Permission to use, copy, modify, and distribute this software for any
00004  * purpose with or without fee is hereby granted, provided that the above
00005  * copyright notice and this permission notice appear in all copies.
00006  *
00007  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
00008  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00009  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
00010  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00011  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00012  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00013  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00014  */
00015 
00016 #ifndef LOGCABIN_STORAGE_LAYOUT_H
00017 #define LOGCABIN_STORAGE_LAYOUT_H
00018 
00019 #include "Storage/FilesystemUtil.h"
00020 
00021 namespace LogCabin {
00022 
00023 // forward declaration
00024 namespace Core {
00025 class Config;
00026 }
00027 
00028 namespace Storage {
00029 
00030 /**
00031  * Encapsulates how LogCabin lays out the filesystem, and it handles locking of
00032  * the storage directory.
00033  *
00034  * The current filesystem layout looks like this:
00035  *
00036  * / - topDir, defined by config option 'storagePath'
00037  *     "server%lu" % serverId/ - serverDir
00038  *         log/ - logDir, Storage::Log implementation-defined
00039  *         snapshot/ -  snapshotDir, contains snapshots
00040  *             snapshot - latest complete snapshot
00041  *             "partial.%010lu.%06lu" % (seconds, micro) - in progress
00042  *         lock - lockFile, ensures only 1 process accesses serverDir a time
00043  */
00044 class Layout {
00045   public:
00046 
00047     /// Default constructor. Call some init method next.
00048     Layout();
00049 
00050     /// Move constructor.
00051     Layout(Layout&& other);
00052 
00053     /// Destructor.
00054     ~Layout();
00055 
00056     /// Move assignment.
00057     Layout& operator=(Layout&& other);
00058 
00059     /**
00060      * Initialize in the normal way for a LogCabin server.
00061      * \param config
00062      *      Server settings: used to extract storage path.
00063      * \param serverId
00064      *      Unique ID for this server.
00065      */
00066     void init(const Core::Config& config, uint64_t serverId);
00067 
00068     /**
00069      * Initialize with a particular storagePath.
00070      * \param storagePath
00071      *      Path for 'topDir'.
00072      * \param serverId
00073      *      Unique ID for this server.
00074      */
00075     void init(const std::string& storagePath, uint64_t serverId);
00076 
00077     /**
00078      * Initialize for unit tests. This will set up the layout in a temporary
00079      * directory, and this class will remove all files in that directory when
00080      * it is destroyed.
00081      * \param serverId
00082      *      Unique ID for this server (in case it matters).
00083      */
00084     void initTemporary(uint64_t serverId = 1);
00085 
00086     /**
00087      * Contains all files.
00088      * Defined by config option 'storagePath'.
00089      */
00090     FilesystemUtil::File topDir;
00091     /**
00092      * Contains all files for this particular server.
00093      * Sits underneath topDir in a directory called "server%lu" % serverId.
00094      */
00095     FilesystemUtil::File serverDir;
00096     /**
00097      * Used to ensure only one process accesses serverDir at a time.
00098      * Sits underneath serverDir in a file called "lock".
00099      */
00100     FilesystemUtil::File lockFile;
00101     /**
00102      * Contains all log files for this particular server.
00103      * Sits underneath serverDir in a directory called "log".
00104      */
00105     FilesystemUtil::File logDir;
00106     /**
00107      * Contains all snapshot files for this particular server.
00108      * Sits underneath serverDir in a directory called "snapshot".
00109      */
00110     FilesystemUtil::File snapshotDir;
00111 
00112   private:
00113     /**
00114      * If true, rm -rf topDir when destroying this class.
00115      */
00116     bool removeAllFiles;
00117 
00118     // Layout is movable but not coypable (the semantics around removeAllFiles
00119     // would be tricky).
00120     Layout(const Layout& other) = delete;
00121     Layout& operator=(const Layout& other) = delete;
00122 };
00123 
00124 } // namespace LogCabin::Storage
00125 } // namespace LogCabin
00126 
00127 #endif /* LOGCABIN_STORAGE_LAYOUT_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines