00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __FILE_H__
00012 #define __FILE_H__
00013
00014 #include "sync.h"
00015
00016 BEGIN_FASTDB_NAMESPACE
00017
00018 #if defined(REPLICATION_SUPPORT)
00019 const int dbModMapBlockBits = 12;
00020 const int dbModMapBlockSize = 1 << dbModMapBlockBits;
00021 #elif defined(NO_MMAP)
00022 const int dbModMapBlockBits = 12;
00023 const int dbModMapBlockSize = 1 << dbModMapBlockBits;
00024 #endif
00025
00026 #ifdef REPLICATION_SUPPORT
00027
00028 class dbFile;
00029 class dbReplicatedDatabase;
00030 class socket_t;
00031
00032 struct ReplicationRequest {
00033 enum {
00034 RR_CONNECT,
00035 RR_RECOVERY,
00036 RR_GET_STATUS,
00037 RR_STATUS,
00038 RR_UPDATE_PAGE,
00039 RR_NEW_ACTIVE_NODE,
00040 RR_CHANGE_ACTIVE_NODE,
00041 RR_CLOSE,
00042 RR_READY
00043 };
00044 byte op;
00045 byte nodeId;
00046 byte status;
00047 int size;
00048 struct {
00049 int updateCount;
00050 int offs;
00051 } page;
00052 };
00053
00054 struct RecoveryRequest {
00055 dbFile* file;
00056 int nodeId;
00057 int nPages;
00058 int* updateCounters;
00059 };
00060 #endif
00061
00062 #ifdef FUZZY_CHECKPOINT
00063 class dbFileWriter;
00064 #endif
00065
00066 class dbFile {
00067 protected:
00068 #ifdef _WIN32
00069 HANDLE fh;
00070 HANDLE mh;
00071 #else
00072 #ifdef USE_SYSV_SHARED_MEMORY
00073 dbSharedMemory shmem;
00074 #endif
00075 int fd;
00076 #endif
00077 char* sharedName;
00078 char* mmapAddr;
00079 size_t mmapSize;
00080 bool readonly;
00081 public:
00082 enum {
00083 ok = 0
00084 };
00085
00086
00087
00088 int create(char const* name, bool noBuffering = true);
00089
00090
00091
00092 int open(char const* fileName, char const* sharedName,
00093 bool readonly, size_t initSize, bool replicationSupport);
00094
00095 void* getAddr() const { return mmapAddr; }
00096 size_t getSize() const { return mmapSize; }
00097 int setSize(size_t size, char const* sharedName, bool initialize = true);
00098 int flush(bool physical = false);
00099 int close();
00100 int erase();
00101 int write(void const* ptr, size_t& writtenBytes, size_t size);
00102 int read(void* ptr, size_t& readBytes, size_t size);
00103 bool write(void const* ptr, size_t size);
00104
00105 static char* errorText(int code, char* buf, size_t bufSize);
00106
00107 #if defined(NO_MMAP) || defined(REPLICATION_SUPPORT)
00108 void markAsDirty(size_t pos, size_t size) {
00109 size_t page = pos >> dbModMapBlockBits;
00110 size_t last = (pos + size + dbModMapBlockSize - 1) >> dbModMapBlockBits;
00111 assert(int(last >> 5) <= pageMapSize);
00112 while (page < last) {
00113 pageMap[page >> 5] |= 1 << (page & 31);
00114 page += 1;
00115 }
00116 }
00117
00118 private:
00119 int* pageMap;
00120 int pageMapSize;
00121 int pageSize;
00122
00123 #ifdef FUZZY_CHECKPOINT
00124 dbFileWriter* writer;
00125 public:
00126 void setCheckpointBufferSize(size_t nPages);
00127 #endif
00128
00129 public:
00130 int updateCounter;
00131
00132 #ifdef REPLICATION_SUPPORT
00133 int* currUpdateCount;
00134 int* diskUpdateCount;
00135 byte* rootPage;
00136 bool doSync;
00137 bool closing;
00138
00139 dbReplicatedDatabase* db;
00140
00141 int getUpdateCountTableSize();
00142 int getMaxPages();
00143
00144 dbMutex replCS;
00145 dbMutex syncCS;
00146
00147 dbThread syncThread;
00148 dbLocalEvent syncEvent;
00149 dbLocalEvent recoveredEvent;
00150 int nRecovered;
00151
00152 static int dbSyncTimeout;
00153
00154 #ifdef _WIN32
00155 HANDLE cfh;
00156 HANDLE cmh;
00157 #else
00158 int cfd;
00159 #endif
00160
00161 static void thread_proc startSyncToDisk(void* arg);
00162 static void thread_proc startRecovery(void* arg);
00163
00164
00165 void doRecovery(int nodeId, int* updateCounters, int nPages);
00166
00167 void syncToDisk();
00168 void startSync();
00169 void stopSync();
00170
00171 public:
00172 void configure(dbReplicatedDatabase* db) {
00173 this->db = db;
00174 }
00175
00176 bool updatePages(socket_t* s, size_t pos, int updateCount, int size);
00177 bool concurrentUpdatePages(socket_t* s, size_t pos, int updateCount, int size);
00178 void recovery(int nodeId, int* updateCounters, int nPages);
00179 #endif
00180
00181
00182 #else
00183 void markAsDirty(size_t, size_t) {}
00184 #endif
00185
00186 bool write(size_t pos, void const* ptr, size_t size);
00187
00188 dbFile();
00189 ~dbFile();
00190 };
00191
00192
00193 END_FASTDB_NAMESPACE
00194
00195 #endif
00196