bt_close.c File Reference

#include <sys/param.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../include/db.h"
#include "btree.h"

Include dependency graph for bt_close.c:

Go to the source code of this file.

Functions

int __bt_close (DB *dbp)
int __bt_sync (DB *dbp, u_int flags) const
static int bt_meta __P ((BTREE *))
static int bt_meta (BTREE *t)


Function Documentation

int __bt_close ( DB dbp  ) 

Definition at line 64 of file bt_close.c.

References __bt_sync(), _btree::bt_cursor, _btree::bt_fd, _btree::bt_mp, _btree::bt_pinned, _btree::bt_rdata, _btree::bt_rkey, DBT::data, free, _cursor::key, mpool_close, mpool_put, NULL, RET_ERROR, RET_SUCCESS, and DBT::size.

Referenced by __bt_open(), __rec_close(), and __rec_open().

00066 {
00067    BTREE *t;
00068    int fd;
00069 
00070    t = dbp->internal;
00071 
00072    /* Toss any page pinned across calls. */
00073    if (t->bt_pinned != NULL) {
00074       mpool_put(t->bt_mp, t->bt_pinned, 0);
00075       t->bt_pinned = NULL;
00076    }
00077 
00078    /* Sync the tree. */
00079    if (__bt_sync(dbp, 0) == RET_ERROR)
00080       return (RET_ERROR);
00081 
00082    /* Close the memory pool. */
00083    if (mpool_close(t->bt_mp) == RET_ERROR)
00084       return (RET_ERROR);
00085 
00086    /* Free random memory. */
00087    if (t->bt_cursor.key.data != NULL) {
00088       free(t->bt_cursor.key.data);
00089       t->bt_cursor.key.size = 0;
00090       t->bt_cursor.key.data = NULL;
00091    }
00092    if (t->bt_rkey.data) {
00093       free(t->bt_rkey.data);
00094       t->bt_rkey.size = 0;
00095       t->bt_rkey.data = NULL;
00096    }
00097    if (t->bt_rdata.data) {
00098       free(t->bt_rdata.data);
00099       t->bt_rdata.size = 0;
00100       t->bt_rdata.data = NULL;
00101    }
00102 
00103    fd = t->bt_fd;
00104    free(t);
00105    free(dbp);
00106    return (close(fd) ? RET_ERROR : RET_SUCCESS);
00107 }

int __bt_sync ( DB dbp,
u_int  flags 
) const

Definition at line 119 of file bt_close.c.

References B_INMEM, B_METADIRTY, B_MODIFIED, B_RDONLY, bt_meta(), _btree::bt_mp, _btree::bt_pinned, errno, F_CLR, F_ISSET, mpool_put, mpool_sync, NULL, RET_ERROR, RET_SUCCESS, and status.

Referenced by __bt_close(), __bt_open(), and __rec_sync().

00122 {
00123    BTREE *t;
00124    int status;
00125 
00126    t = dbp->internal;
00127 
00128    /* Toss any page pinned across calls. */
00129    if (t->bt_pinned != NULL) {
00130       mpool_put(t->bt_mp, t->bt_pinned, 0);
00131       t->bt_pinned = NULL;
00132    }
00133 
00134    /* Sync doesn't currently take any flags. */
00135    if (flags != 0) {
00136       errno = EINVAL;
00137       return (RET_ERROR);
00138    }
00139 
00140    if (F_ISSET(t, B_INMEM | B_RDONLY) || !F_ISSET(t, B_MODIFIED))
00141       return (RET_SUCCESS);
00142 
00143    if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
00144       return (RET_ERROR);
00145 
00146    if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
00147       F_CLR(t, B_MODIFIED);
00148 
00149    return (status);
00150 }

static int bt_meta __P ( (BTREE *)   )  [static]

static int bt_meta ( BTREE t  )  [static]

Definition at line 162 of file bt_close.c.

References BTREEMAGIC, BTREEVERSION, F_ISSET, _btmeta::flags, _btmeta::free, _btmeta::magic, MPOOL_DIRTY, mpool_get, mpool_put, _btmeta::nrecs, NULL, P_META, _btmeta::psize, RET_ERROR, RET_SUCCESS, SAVEMETA, and _btmeta::version.

Referenced by __bt_sync().

00164 {
00165    BTMETA m;
00166    void *p;
00167 
00168    if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)
00169       return (RET_ERROR);
00170 
00171    /* Fill in metadata. */
00172    m.magic = BTREEMAGIC;
00173    m.version = BTREEVERSION;
00174    m.psize = t->bt_psize;
00175    m.free = t->bt_free;
00176    m.nrecs = t->bt_nrecs;
00177    m.flags = F_ISSET(t, SAVEMETA);
00178 
00179    memmove(p, &m, sizeof(BTMETA));
00180    mpool_put(t->bt_mp, p, MPOOL_DIRTY);
00181    return (RET_SUCCESS);
00182 }


Generated on Thu Apr 16 06:29:14 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6