1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <memory.h> #include "HashFile.h"
int hashfile_creat(const char *filename, mode_t mode, int reclen, int total_rec_num) { struct HashFileHeader hfh; int fd; int rtn; char *buf; int i = 0; hfh.sig = 31415926; hfh.reclen = reclen; hfh.total_rec_num = total_rec_num; hfh.current_rec_num = 0; fd = creat(filename, mode); if (fd != -1) { rtn = write(fd, &hfh, sizeof(struct HashFileHeader)); if (rtn != -1) { buf = (char *)malloc((reclen + sizeof(struct CFTag)) * total_rec_num); memset(buf, 0, (reclen + sizeof(struct CFTag)) * total_rec_num); rtn = write(fd, buf, (reclen + sizeof(struct CFTag)) * total_rec_num); free(buf); } close(fd); return rtn; } else { close(fd); return -1; } }
int hashfile_open(const char *filename, int flags, mode_t mode) { int fd = open(filename, flags, mode); struct HashFileHeader hfh; if (read(fd, &hfh, sizeof(struct HashFileHeader)) != -1) { lseek(fd, 0, SEEK_SET); if (hfh.sig == 31415926) return fd; else return -1; } else return -1; } int hashfile_close(int fd) { return close(fd); } int hashfile_read(int fd, int keyoffset, int keylen, void *buf) { struct HashFileHeader hfh; readHashFileHeader(fd, &hfh); int offset = hashfile_findrec(fd, keyoffset, keylen, buf); if (offset != -1) { lseek(fd, offset + sizeof(struct CFTag), SEEK_SET); return read(fd, buf, hfh.reclen); } else { return -1; } } int hashfile_write(int fd, int keyoffset, int keylen, void *buf) { return hashfile_saverec(fd, keyoffset, keylen, buf); } int hashfile_delrec(int fd, int keyoffset, int keylen, void *buf) { int offset; offset = hashfile_findrec(fd, keyoffset, keylen, buf); if (offset != -1) { struct CFTag tag; read(fd, &tag, sizeof(struct CFTag)); tag.free = 0; lseek(fd, offset, SEEK_SET); write(fd, &tag, sizeof(struct CFTag)); struct HashFileHeader hfh; readHashFileHeader(fd, &hfh); int addr = hash(keyoffset, keylen, buf, hfh.total_rec_num); offset = sizeof(struct HashFileHeader) + addr * (hfh.reclen + sizeof(struct CFTag)); if (lseek(fd, offset, SEEK_SET) == -1) return -1; read(fd, &tag, sizeof(struct CFTag)); tag.collision--; lseek(fd, offset, SEEK_SET); write(fd, &tag, sizeof(struct CFTag)); hfh.current_rec_num--; lseek(fd, 0, SEEK_SET); write(fd, &hfh, sizeof(struct HashFileHeader)); } else { return -1; } }
int hashfile_findrec(int fd, int keyoffset, int keylen, void *buf) { struct HashFileHeader hfh; readHashFileHeader(fd, &hfh); int addr = hash(keyoffset, keylen, buf, hfh.total_rec_num); int offset = sizeof(struct HashFileHeader) + addr * (hfh.reclen + sizeof(struct CFTag)); if (lseek(fd, offset, SEEK_SET) == -1) return -1; struct CFTag tag; read(fd, &tag, sizeof(struct CFTag)); char count = tag.collision; if (count == 0) return -1; recfree: if (tag.free == 0) { offset += hfh.reclen + sizeof(struct CFTag); if (lseek(fd, offset, SEEK_SET) == -1) return -1; read(fd, &tag, sizeof(struct CFTag)); goto recfree; } else { char *p = (char *)malloc(hfh.reclen * sizeof(char)); read(fd, p, hfh.reclen); char *p1, *p2; p1 = (char *)buf + keyoffset; p2 = p + keyoffset; int j = 0; while ((*p1 == *p2) && (j < keylen)) { p1++; p2++; j++; } if (j == keylen) { free(p); p = NULL; return (offset); } else { if (addr == hash(keyoffset, keylen, p, hfh.total_rec_num)) { count--; if (count == 0) { free(p); p = NULL; return -1; } } free(p); p = NULL; offset += hfh.reclen + sizeof(struct CFTag); if (lseek(fd, offset, SEEK_SET) == -1) return -1; read(fd, &tag, sizeof(struct CFTag)); goto recfree; } } } int hashfile_saverec(int fd, int keyoffset, int keylen, void *buf) { if (checkHashFileFull(fd)) { return -1; } struct HashFileHeader hfh; readHashFileHeader(fd, &hfh); int addr = hash(keyoffset, keylen, buf, hfh.total_rec_num); int offset = sizeof(struct HashFileHeader) + addr * (hfh.reclen + sizeof(struct CFTag)); if (lseek(fd, offset, SEEK_SET) == -1) return -1; struct CFTag tag; read(fd, &tag, sizeof(struct CFTag)); tag.collision++; lseek(fd, sizeof(struct CFTag) * (-1), SEEK_CUR); write(fd, &tag, sizeof(struct CFTag)); while (tag.free != 0) { offset += hfh.reclen + sizeof(struct CFTag); if (offset >= lseek(fd, 0, SEEK_END)) offset = sizeof(struct HashFileHeader); if (lseek(fd, offset, SEEK_SET) == -1) return -1; read(fd, &tag, sizeof(struct CFTag)); } tag.free = -1; lseek(fd, sizeof(struct CFTag) * (-1), SEEK_CUR); write(fd, &tag, sizeof(struct CFTag)); write(fd, buf, hfh.reclen); hfh.current_rec_num++; lseek(fd, 0, SEEK_SET); return write(fd, &hfh, sizeof(struct HashFileHeader)); }
int hash(int keyoffset, int keylen, void *buf, int total_rec_num) { int i = 0; char *p = (char *)buf + keyoffset; int addr = 0; for (i = 0; i < keylen; i++) { addr += (int)(*p); p++; } return addr % (int)(total_rec_num * COLLISIONFACTOR); }
int checkHashFileFull(int fd) { struct HashFileHeader hfh; readHashFileHeader(fd, &hfh); if (hfh.current_rec_num < hfh.total_rec_num) return 0; else return 1; } int readHashFileHeader(int fd, struct HashFileHeader *hfh) { lseek(fd, 0, SEEK_SET); return read(fd, hfh, sizeof(struct HashFileHeader)); }
|