Skip to content

Commit

Permalink
Merge pull request #2639 from wiredtiger/wt-2540
Browse files Browse the repository at this point in the history
WT-2540: Separate stream and file handle methods
  • Loading branch information
michaelcahill committed Apr 12, 2016
2 parents c3643a8 + 49f7a93 commit 6f22861
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 383 deletions.
1 change: 1 addition & 0 deletions build_win/filelist.win
Expand Up @@ -105,6 +105,7 @@ src/os_common/filename.c
src/os_common/os_abort.c
src/os_common/os_alloc.c
src/os_common/os_fhandle.c
src/os_common/os_fs_init.c
src/os_common/os_fs_inmemory.c
src/os_common/os_fs_stdio.c
src/os_common/os_getline.c
Expand Down
1 change: 1 addition & 0 deletions dist/filelist
Expand Up @@ -105,6 +105,7 @@ src/os_common/filename.c
src/os_common/os_abort.c
src/os_common/os_alloc.c
src/os_common/os_fhandle.c
src/os_common/os_fs_init.c
src/os_common/os_fs_inmemory.c
src/os_common/os_fs_stdio.c
src/os_common/os_getline.c
Expand Down
1 change: 1 addition & 0 deletions src/include/extern.h
Expand Up @@ -772,6 +772,7 @@ extern int __wt_win_map_preload( WT_SESSION_IMPL *session, WT_FH *fh, const void
extern int __wt_win_map_unmap(WT_SESSION_IMPL *session, WT_FH *fh, void *map, size_t len, void **mappingcookie);
extern uint64_t __wt_strtouq(const char *nptr, char **endptr, int base);
extern void __wt_abort(WT_SESSION_IMPL *session) WT_GCC_FUNC_DECL_ATTRIBUTE((noreturn));
extern void __wt_fh_method_init(WT_FH *fh);
extern void __wt_free_int(WT_SESSION_IMPL *session, const void *p_arg);
extern void __wt_posix_handle_allocate_configure(WT_SESSION_IMPL *session, WT_FH *fh);
extern void __wt_sleep(uint64_t seconds, uint64_t micro_seconds);
Expand Down
45 changes: 35 additions & 10 deletions src/os_common/os_fhandle.c
Expand Up @@ -142,6 +142,37 @@ err: __wt_scr_free(session, &tmp);
#endif
}

/*
* __open_refresh --
* Refresh a file handle.
*/
static inline int
__open_refresh(WT_SESSION_IMPL *session,
WT_FH *fh, const char *name, uint32_t file_type, uint32_t flags)
{
WT_DECL_RET;

/*
* !!!
* The in-memory file handle open must repeat if the reference count
* goes to 0, no other file handle requires refresh.
*/
if (!F_ISSET(fh, WT_FH_IN_MEMORY) || fh->ref > 1)
return (0);

__wt_fh_method_init(fh);

WT_ERR(S2C(session)->handle_open(session, fh, name, file_type, flags));
return (0);

err: /*
* We've already incremented the handle's reference count, close the
* handle to discard that reference.
*/
WT_TRET(fh->fh_close(session, fh));
return (ret);
}

/*
* __wt_open --
* Open a file handle.
Expand All @@ -167,22 +198,16 @@ __wt_open(WT_SESSION_IMPL *session,

/* Check if the handle is already open. */
if (__wt_handle_search(session, name, true, NULL, &fh)) {
/*
* XXX
* The in-memory implementation has to reset the file offset
* when a file is re-opened (which obviously also depends on
* in-memory configurations never opening a file in more than
* one thread at a time). This needs to be fixed.
*/
if (F_ISSET(fh, WT_FH_IN_MEMORY) && fh->ref == 1)
fh->off = 0;
WT_RET(__open_refresh(session, fh, name, file_type, flags));

*fhp = fh;
return (0);
}

/* Allocate a structure and set the name. */
/* Allocate and initialize the handle. */
WT_ERR(__wt_calloc_one(session, &fh));
WT_ERR(__wt_strdup(session, name, &fh->name));
__wt_fh_method_init(fh);

/*
* If this is a read-only connection, open all files read-only except
Expand Down
228 changes: 228 additions & 0 deletions src/os_common/os_fs_init.c
@@ -0,0 +1,228 @@
/*-
* Copyright (c) 2014-2016 MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/

#include "wt_internal.h"

/*
* __notsup_handle_advise --
* POSIX fadvise.
*/
static int
__notsup_handle_advise(WT_SESSION_IMPL *session,
WT_FH *fh, wt_off_t offset, wt_off_t len, int advice)
{
WT_UNUSED(session);
WT_UNUSED(fh);
WT_UNUSED(offset);
WT_UNUSED(len);
WT_UNUSED(advice);

/* Quietly fail, callers expect not-supported failures. */
return (ENOTSUP);
}

/*
* __notsup_handle_allocate --
* POSIX fallocate.
*/
static int
__notsup_handle_allocate(
WT_SESSION_IMPL *session, WT_FH *fh, wt_off_t offset, wt_off_t len)
{
WT_UNUSED(offset);
WT_UNUSED(len);
WT_RET_MSG(session, ENOTSUP, "%s: handle-allocate", fh->name);
}

/*
* __notsup_handle_close --
* ANSI C close/fclose.
*/
static int
__notsup_handle_close(WT_SESSION_IMPL *session, WT_FH *fh)
{
WT_RET_MSG(session, ENOTSUP, "%s: handle-close", fh->name);
}

/*
* __notsup_handle_getc --
* ANSI C fgetc.
*/
static int
__notsup_handle_getc(WT_SESSION_IMPL *session, WT_FH *fh, int *chp)
{
WT_UNUSED(chp);
WT_RET_MSG(session, ENOTSUP, "%s: handle-getc", fh->name);
}

/*
* __notsup_handle_lock --
* Lock/unlock a file.
*/
static int
__notsup_handle_lock(WT_SESSION_IMPL *session, WT_FH *fh, bool lock)
{
WT_UNUSED(lock);
WT_RET_MSG(session, ENOTSUP, "%s: handle-lock", fh->name);
}

/*
* __notsup_handle_map --
* Map a file.
*/
static int
__notsup_handle_map(WT_SESSION_IMPL *session,
WT_FH *fh, void *p, size_t *lenp, void **mappingcookie)
{
WT_UNUSED(p);
WT_UNUSED(lenp);
WT_UNUSED(mappingcookie);
WT_RET_MSG(session, ENOTSUP, "%s: handle-map", fh->name);
}

/*
* __notsup_handle_map_discard --
* Discard a section of a mapped region.
*/
static int
__notsup_handle_map_discard(
WT_SESSION_IMPL *session, WT_FH *fh, void *p, size_t len)
{
WT_UNUSED(p);
WT_UNUSED(len);
WT_RET_MSG(session, ENOTSUP, "%s: handle-map-discard", fh->name);
}

/*
* __notsup_handle_map_preload --
* Preload a section of a mapped region.
*/
static int
__notsup_handle_map_preload(
WT_SESSION_IMPL *session, WT_FH *fh, const void *p, size_t len)
{
WT_UNUSED(p);
WT_UNUSED(len);
WT_RET_MSG(session, ENOTSUP, "%s: handle-map-preload", fh->name);
}

/*
* __notsup_handle_map_unmap --
* Unmap a file.
*/
static int
__notsup_handle_map_unmap(WT_SESSION_IMPL *session,
WT_FH *fh, void *p, size_t len, void **mappingcookie)
{
WT_UNUSED(p);
WT_UNUSED(len);
WT_UNUSED(mappingcookie);
WT_RET_MSG(session, ENOTSUP, "%s: handle-map-unmap", fh->name);
}

/*
* __notsup_handle_printf --
* ANSI C vfprintf.
*/
static int
__notsup_handle_printf(
WT_SESSION_IMPL *session, WT_FH *fh, const char *fmt, va_list ap)
{
WT_UNUSED(fmt);
WT_UNUSED(ap);
WT_RET_MSG(session, ENOTSUP, "%s: handle-printf", fh->name);
}

/*
* __notsup_handle_read --
* POSIX pread.
*/
static int
__notsup_handle_read(
WT_SESSION_IMPL *session, WT_FH *fh, wt_off_t offset, size_t len, void *buf)
{
WT_UNUSED(offset);
WT_UNUSED(len);
WT_UNUSED(buf);
WT_RET_MSG(session, ENOTSUP, "%s: handle-read", fh->name);
}

/*
* __notsup_handle_size --
* Get the size of a file in bytes, by file handle.
*/
static int
__notsup_handle_size(WT_SESSION_IMPL *session, WT_FH *fh, wt_off_t *sizep)
{
WT_UNUSED(sizep);
WT_RET_MSG(session, ENOTSUP, "%s: handle-size", fh->name);
}

/*
* __notsup_handle_sync --
* POSIX fflush/fsync.
*/
static int
__notsup_handle_sync(WT_SESSION_IMPL *session, WT_FH *fh, bool block)
{
WT_UNUSED(block);
WT_RET_MSG(session, ENOTSUP, "%s: handle-sync", fh->name);
}

/*
* __notsup_handle_truncate --
* POSIX ftruncate.
*/
static int
__notsup_handle_truncate(WT_SESSION_IMPL *session, WT_FH *fh, wt_off_t len)
{
WT_UNUSED(len);
WT_RET_MSG(session, ENOTSUP, "%s: handle-truncate", fh->name);
}

/*
* __notsup_handle_write --
* POSIX pwrite.
*/
static int
__notsup_handle_write(WT_SESSION_IMPL *session,
WT_FH *fh, wt_off_t offset, size_t len, const void *buf)
{
WT_UNUSED(offset);
WT_UNUSED(len);
WT_UNUSED(buf);
WT_RET_MSG(session, ENOTSUP, "%s: handle-write", fh->name);
}

/*
* __wt_fh_method_init --
* Initialize the WT_FH structure's methods to not-supported.
*/
void
__wt_fh_method_init(WT_FH *fh)
{
/*
* Set up the initial set of handle methods to standard "not-supported"
* functions, the underlying open functions turn on supported functions.
*/
fh->fh_advise = __notsup_handle_advise;
fh->fh_allocate = __notsup_handle_allocate;
fh->fh_close = __notsup_handle_close;
fh->fh_getc = __notsup_handle_getc;
fh->fh_lock = __notsup_handle_lock;
fh->fh_map = __notsup_handle_map;
fh->fh_map_discard = __notsup_handle_map_discard;
fh->fh_map_preload = __notsup_handle_map_preload;
fh->fh_map_unmap = __notsup_handle_map_unmap;
fh->fh_printf = __notsup_handle_printf;
fh->fh_read = __notsup_handle_read;
fh->fh_size = __notsup_handle_size;
fh->fh_sync = __notsup_handle_sync;
fh->fh_truncate = __notsup_handle_truncate;
fh->fh_write = __notsup_handle_write;
}
48 changes: 22 additions & 26 deletions src/os_common/os_fs_inmemory.c
Expand Up @@ -162,22 +162,6 @@ err: __wt_spin_unlock(session, &im->lock);
return (ret);
}

/*
* __im_handle_advise --
* POSIX fadvise.
*/
static int
__im_handle_advise(WT_SESSION_IMPL *session,
WT_FH *fh, wt_off_t offset, wt_off_t len, int advice)
{
WT_UNUSED(session);
WT_UNUSED(fh);
WT_UNUSED(offset);
WT_UNUSED(len);
WT_UNUSED(advice);
return (ENOTSUP);
}

/*
* __im_handle_close --
* ANSI C close/fclose.
Expand Down Expand Up @@ -218,6 +202,7 @@ __im_handle_getc(WT_SESSION_IMPL *session, WT_FH *fh, int *chp)
static int
__im_handle_lock(WT_SESSION_IMPL *session, WT_FH *fh, bool lock)
{
/* Locks are always granted. */
WT_UNUSED(session);
WT_UNUSED(fh);
WT_UNUSED(lock);
Expand Down Expand Up @@ -410,19 +395,30 @@ __im_handle_open(WT_SESSION_IMPL *session,
WT_UNUSED(file_type);
WT_UNUSED(flags);

/*
* Unlike other file handle open implementations, the in-memory version
* is called whenever the WT_FH structure reference count goes to 0.
* This is because the in-memory implementation reuses WT_FH structures,
* and so we have to reset the file offset and potentially the list of
* functions, in the case of the file being opened in a different way.
*/
fh->off = 0;
F_SET(fh, WT_FH_IN_MEMORY);

fh->fh_advise = __im_handle_advise;
fh->fh_close = __im_handle_close;
fh->fh_getc = __im_handle_getc;
fh->fh_lock = __im_handle_lock;
fh->fh_printf = __im_handle_printf;
fh->fh_read = __im_handle_read;
fh->fh_size = __im_handle_size;
fh->fh_sync = __im_handle_sync;
fh->fh_truncate = __im_handle_truncate;
fh->fh_write = __im_handle_write;
if (LF_ISSET(WT_STREAM_APPEND | WT_STREAM_READ | WT_STREAM_WRITE)) {
fh->fh_close = __im_handle_close;
fh->fh_getc = __im_handle_getc;
fh->fh_printf = __im_handle_printf;
fh->fh_sync = __im_handle_sync;
} else {
fh->fh_close = __im_handle_close;
fh->fh_lock = __im_handle_lock;
fh->fh_read = __im_handle_read;
fh->fh_size = __im_handle_size;
fh->fh_sync = __im_handle_sync;
fh->fh_truncate = __im_handle_truncate;
fh->fh_write = __im_handle_write;
}

return (0);
}
Expand Down

0 comments on commit 6f22861

Please sign in to comment.