libmdb: Don't open datafile twice when not needed.

If the database is opened with MDB_RDONLY or MDB_NOSYNC,
instead use the same file descriptor for me_mfd and me_fd.

Also factor out Windows/Unix error handling after open.
vmware
Hallvard Furuseth 13 years ago
parent 6d0b424dbe
commit 8e1ebbb4a8
  1. 34
      libraries/libmdb/mdb.c

@ -2983,41 +2983,38 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
len = OPEN_ALWAYS; len = OPEN_ALWAYS;
} }
mode = FILE_ATTRIBUTE_NORMAL; mode = FILE_ATTRIBUTE_NORMAL;
if ((env->me_fd = CreateFile(dpath, oflags, FILE_SHARE_READ|FILE_SHARE_WRITE, env->me_fd = CreateFile(dpath, oflags, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, len, mode, NULL)) == INVALID_HANDLE_VALUE) { NULL, len, mode, NULL);
rc = ErrCode();
goto leave;
}
#else #else
if (F_ISSET(flags, MDB_RDONLY)) if (F_ISSET(flags, MDB_RDONLY))
oflags = O_RDONLY; oflags = O_RDONLY;
else else
oflags = O_RDWR | O_CREAT; oflags = O_RDWR | O_CREAT;
if ((env->me_fd = open(dpath, oflags, mode)) == -1) { env->me_fd = open(dpath, oflags, mode);
#endif
if (env->me_fd == INVALID_HANDLE_VALUE) {
rc = ErrCode(); rc = ErrCode();
goto leave; goto leave;
} }
#endif
if ((rc = mdb_env_open2(env, flags)) == MDB_SUCCESS) { if ((rc = mdb_env_open2(env, flags)) == MDB_SUCCESS) {
if (flags & (MDB_RDONLY|MDB_NOSYNC)) {
env->me_mfd = env->me_fd;
} else {
/* synchronous fd for meta writes */ /* synchronous fd for meta writes */
#ifdef _WIN32 #ifdef _WIN32
if (!(flags & (MDB_RDONLY|MDB_NOSYNC))) env->me_mfd = CreateFile(dpath, oflags,
mode |= FILE_FLAG_WRITE_THROUGH; FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, len,
if ((env->me_mfd = CreateFile(dpath, oflags, FILE_SHARE_READ|FILE_SHARE_WRITE, mode | FILE_FLAG_WRITE_THROUGH, NULL);
NULL, len, mode, NULL)) == INVALID_HANDLE_VALUE) {
rc = ErrCode();
goto leave;
}
#else #else
if (!(flags & (MDB_RDONLY|MDB_NOSYNC))) env->me_mfd = open(dpath, oflags | MDB_DSYNC, mode);
oflags |= MDB_DSYNC; #endif
if ((env->me_mfd = open(dpath, oflags, mode)) == -1) { if (env->me_mfd == INVALID_HANDLE_VALUE) {
rc = ErrCode(); rc = ErrCode();
goto leave; goto leave;
} }
#endif }
env->me_path = strdup(path); env->me_path = strdup(path);
DPRINTF("opened dbenv %p", (void *) env); DPRINTF("opened dbenv %p", (void *) env);
pthread_key_create(&env->me_txkey, mdb_env_reader_dest); pthread_key_create(&env->me_txkey, mdb_env_reader_dest);
@ -3072,6 +3069,7 @@ mdb_env_close(MDB_env *env)
if (env->me_map) { if (env->me_map) {
munmap(env->me_map, env->me_mapsize); munmap(env->me_map, env->me_mapsize);
} }
if (env->me_mfd != env->me_fd)
close(env->me_mfd); close(env->me_mfd);
close(env->me_fd); close(env->me_fd);
if (env->me_txns) { if (env->me_txns) {

Loading…
Cancel
Save