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. 42
      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) {
/* synchronous fd for meta writes */ if (flags & (MDB_RDONLY|MDB_NOSYNC)) {
env->me_mfd = env->me_fd;
} else {
/* 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;
if ((env->me_mfd = open(dpath, oflags, mode)) == -1) {
rc = ErrCode();
goto leave;
}
#endif #endif
if (env->me_mfd == INVALID_HANDLE_VALUE) {
rc = ErrCode();
goto leave;
}
}
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,7 +3069,8 @@ 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);
} }
close(env->me_mfd); if (env->me_mfd != env->me_fd)
close(env->me_mfd);
close(env->me_fd); close(env->me_fd);
if (env->me_txns) { if (env->me_txns) {
pid_t pid = getpid(); pid_t pid = getpid();

Loading…
Cancel
Save