Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

NVcontainer.h

Go to the documentation of this file.
00001 #ifndef _NVcontainer_h_
00002 #define _NVcontainer_h_
00003 
00004 #include <fcntl.h>
00005 #include <sys/file.h>
00006 #include <sys/param.h>
00007 #include <unistd.h>
00008 
00009 #include "Debug.h"
00010 
00011 #define NVcontainer_LOCKSTACKSIZE 16
00012 
00013 typedef unsigned long nvtime_t;
00014 typedef unsigned long nvoff_t;
00015 
00016 extern nvtime_t nvtime(nvtime_t * nvt);
00017 
00029 class NVcontainer {
00030         struct Header {
00031                 unsigned int hlen;      /* 32 */
00032                 unsigned int version;   /* 32 */
00033                 nvoff_t size;           /* 32/64 */
00034                 nvtime_t mtime;         /* 32/64 */
00035                 nvoff_t freelist;       /* 32/64 */
00036                 nvoff_t bytes_free;     /* 32/64 */
00037                 nvoff_t userdata;       /* 32/64 */
00038                  Header() {
00039                         clear();
00040                 } void clear() {
00041                         hlen = sizeof(Header);
00042                         version = 2;
00043                         size = hlen;
00044                         mtime = 0;
00045                         freelist = userdata = 0;
00046                         bytes_free = 0;
00047         }};
00048         struct FreeList {
00049                 nvoff_t next;   /* 32/64 */
00050                 nvoff_t size;   /* 32/64 */
00051         };
00052         int lck_stack[NVcontainer_LOCKSTACKSIZE];
00053         int lck_stackp;
00054 
00055         char mem_fn[MAXPATHLEN];
00056         int mem_fd;
00057         Header *mem_hdr;
00058 
00059         FreeList *o2fl(nvoff_t o) {
00060                 return (FreeList *) (o ? mem_p + o : NULL);
00061         }
00062         nvoff_t fl2o(FreeList * r) {
00063                 return r ? (char *) r - mem_p : 0;
00064         }
00065 
00066       protected:
00067         NVcontainer(void);
00068         NVcontainer(const char *dbname, int flags = 0);
00069         virtual ~ NVcontainer(void);
00070 
00071         char *mem_p;
00072         size_t mem_sz;
00073 
00074         /*
00075          * The method make_current map the database file into shared memory.
00076          * It does some sanety checks. If a error occours then the database
00077          * file ist renamed and close with rename_to_bad_and_close. If the
00078          * database filesize is changed, then it is necessary to 
00079          * \throw Error and SystemError
00080          */
00081         virtual void make_current(void);
00082 
00083         /*
00084          * Check if the the the file size is equal the memory mapped size.
00085          */
00086         virtual int is_current(void) {
00087                 return mem_sz == mem_hdr->size;
00088         }
00089         virtual size_t resize(size_t nsz);
00090 
00091         nvoff_t getdata();
00092         nvoff_t getdatap();
00093         void setdata(nvoff_t d);
00094 
00095         nvoff_t nvalloc(size_t rsz);
00096         void nvfree(nvoff_t f);
00097       public:
00098         enum {
00099                 UnLock = F_UNLCK,
00100                 ShrdLock = F_RDLCK,
00101                 ExclLock = F_WRLCK
00102         };
00103         enum {
00104                 Block = 0x0,
00105                 NoBlock = 0x1
00106         };
00107 
00108         int lock(int command, int block = Block);
00109         int get_lock(void);
00110 
00111         virtual void open(const char *dbname, int flags = 0);
00112         virtual int is_open(void);
00113         
00114         /*
00115          * Close the file and reset the internal data.
00116          */
00117         virtual void close(void);
00118 
00119         /*
00120          * If th sanity check signals a bad state of the database file,
00121          * then the rename_to_bad_close methods tries to rename the database.
00122          * It append .bad to the current filename. 
00123          * \author Herbert Straub
00124          * \date 2004
00125          */
00126         void rename_to_bad_and_close (void);
00127 
00128         void setmtime(nvtime_t tm, int force = 0);
00129         void getmtime(nvtime_t * tm);
00130 };
00131 
00132 /* We should add a general iterator class */
00133 /*
00134 class NVcontainterIter {
00135 ...
00136 };
00137 */
00138 
00139 #endif

Generated on Sun Oct 24 21:08:19 2004 for NewsCache by doxygen 1.3.6-20040222