📄 qam_files.c
字号:
if (array->low_extent > extid || array->hi_extent < extid) array = &qp->array2; offset = extid - array->low_extent; DB_ASSERT(offset >= 0 && (unsigned) offset < array->n_extent);#if CONFIG_TEST real_name = NULL; /* Find the real name of the file. */ snprintf(buf, sizeof(buf), QUEUE_EXTENT, qp->dir, PATH_SEPARATOR[0], qp->name, extid); if ((ret = __db_appname(dbenv, DB_APP_DATA, buf, 0, NULL, &real_name)) != 0) goto err;#endif /* * The log must be flushed before the file is deleted. We depend on * the log record of the last delete to recreate the file if we crash. */ if (LOGGING_ON(dbenv) && (ret = dbenv->log_flush(dbenv, NULL)) != 0) goto err; mpf = array->mpfarray[offset].mpf; array->mpfarray[offset].mpf = NULL; mpf->set_unlink(mpf, 1); if ((ret = mpf->close(mpf, 0)) != 0) goto err; /* * If the file is at the bottom of the array * shift things down and adjust the end points. */ if (offset == 0) { memmove(array->mpfarray, &array->mpfarray[1], (array->hi_extent - array->low_extent) * sizeof(array->mpfarray[0])); array->mpfarray[ array->hi_extent - array->low_extent].mpf = NULL; if (array->low_extent != array->hi_extent) array->low_extent++; } else { if (extid == array->hi_extent) array->hi_extent--; }err: MUTEX_THREAD_UNLOCK(dbenv, dbp->mutexp);#if CONFIG_TEST if (real_name != NULL) __os_free(dbenv, real_name);#endif return (ret);}/* * __qam_sync -- * Flush the database cache. * * PUBLIC: int __qam_sync __P((DB *, u_int32_t)); */int__qam_sync(dbp, flags) DB *dbp; u_int32_t flags;{ DB_ENV *dbenv; DB_MPOOLFILE *mpf; MPFARRAY *array; QUEUE *qp; QUEUE_FILELIST *filelist; struct __qmpf *mpfp; u_int32_t i; int done, ret; dbenv = dbp->dbenv; mpf = dbp->mpf; PANIC_CHECK(dbenv); DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->sync"); if ((ret = __db_syncchk(dbp, flags)) != 0) return (ret); /* Read-only trees never need to be sync'd. */ if (F_ISSET(dbp, DB_AM_RDONLY)) return (0); /* If the tree was never backed by a database file, we're done. */ if (F_ISSET(dbp, DB_AM_INMEM)) return (0); /* Flush any dirty pages from the cache to the backing file. */ if ((ret = mpf->sync(dbp->mpf)) != 0) return (ret); qp = (QUEUE *)dbp->q_internal; if (qp->page_ext == 0) return (0); /* We do this for the side effect of opening all active extents. */ if ((ret = __qam_gen_filelist(dbp, &filelist)) != 0) return (ret); if (filelist == NULL) return (0); __os_free(dbp->dbenv, filelist); done = 0; qp = (QUEUE *)dbp->q_internal; array = &qp->array1; MUTEX_THREAD_LOCK(dbenv, dbp->mutexp);again: mpfp = array->mpfarray; for (i = array->low_extent; i <= array->hi_extent; i++, mpfp++) if ((mpf = mpfp->mpf) != NULL) { if ((ret = mpf->sync(mpf)) != 0) goto err; /* * If we are the only ones with this file open * then close it so it might be removed. */ if (mpfp->pinref == 0) { mpfp->mpf = NULL; if ((ret = mpf->close(mpf, 0)) != 0) goto err; } } if (done == 0 && qp->array2.n_extent != 0) { array = &qp->array2; done = 1; goto again; }err: MUTEX_THREAD_UNLOCK(dbenv, dbp->mutexp); return (ret);}/* * __qam_gen_filelist -- generate a list of extent files. * Another thread may close the handle so this should only * be used single threaded or with care. * * PUBLIC: int __qam_gen_filelist __P(( DB *, QUEUE_FILELIST **)); */int__qam_gen_filelist(dbp, filelistp) DB *dbp; QUEUE_FILELIST **filelistp;{ DB_ENV *dbenv; DB_MPOOLFILE *mpf; QUEUE *qp; QMETA *meta; db_pgno_t i, last, start; db_recno_t current, first; QUEUE_FILELIST *fp; int ret; dbenv = dbp->dbenv; mpf = dbp->mpf; qp = (QUEUE *)dbp->q_internal; *filelistp = NULL; if (qp->page_ext == 0) return (0); /* This may happen during metapage recovery. */ if (qp->name == NULL) return (0); /* Find out the page number of the last page in the database. */ i = PGNO_BASE_MD; if ((ret = mpf->get(mpf, &i, 0, &meta)) != 0) return (ret); current = meta->cur_recno; first = meta->first_recno; if ((ret = mpf->put(mpf, meta, 0)) != 0) return (ret); last = QAM_RECNO_PAGE(dbp, current); start = QAM_RECNO_PAGE(dbp, first); /* Allocate the worst case plus 1 for null termination. */ if (last >= start) ret = last - start + 2; else ret = last + (QAM_RECNO_PAGE(dbp, UINT32_T_MAX) - start) + 1; if ((ret = __os_calloc(dbenv, ret, sizeof(QUEUE_FILELIST), filelistp)) != 0) return (ret); fp = *filelistp; i = start;again: for (; i <= last; i += qp->page_ext) { if ((ret = __qam_fprobe(dbp, i, &fp->mpf, QAM_PROBE_MPF, 0)) != 0) { if (ret == ENOENT) continue; return (ret); } fp->id = (i - 1) / qp->page_ext; fp++; } if (last < start) { i = 1; start = 0; goto again; } return (0);}/* * __qam_extent_names -- generate a list of extent files names. * * PUBLIC: int __qam_extent_names __P((DB_ENV *, char *, char ***)); */int__qam_extent_names(dbenv, name, namelistp) DB_ENV *dbenv; char *name; char ***namelistp;{ DB *dbp; QUEUE *qp; QUEUE_FILELIST *filelist, *fp; char buf[MAXPATHLEN], *dir, **cp, *freep; int cnt, len, ret; *namelistp = NULL; filelist = NULL; if ((ret = db_create(&dbp, dbenv, 0)) != 0) return (ret); if ((ret = __db_open(dbp, NULL, name, NULL, DB_QUEUE, DB_RDONLY, 0)) != 0) return (ret); qp = dbp->q_internal; if (qp->page_ext == 0) goto done; if ((ret = __qam_gen_filelist(dbp, &filelist)) != 0) goto done; if (filelist == NULL) goto done; cnt = 0; for (fp = filelist; fp->mpf != NULL; fp++) cnt++; dir = ((QUEUE *)dbp->q_internal)->dir; name = ((QUEUE *)dbp->q_internal)->name; /* QUEUE_EXTENT contains extra chars, but add 6 anyway for the int. */ len = (u_int32_t)(cnt * (sizeof(**namelistp) + strlen(QUEUE_EXTENT) + strlen(dir) + strlen(name) + 6)); if ((ret = __os_malloc(dbp->dbenv, len, namelistp)) != 0) goto done; cp = *namelistp; freep = (char *)(cp + cnt + 1); for (fp = filelist; fp->mpf != NULL; fp++) { snprintf(buf, sizeof(buf), QUEUE_EXTENT, dir, PATH_SEPARATOR[0], name, fp->id); len = (u_int32_t)strlen(buf); *cp++ = freep; strcpy(freep, buf); freep += len + 1; } *cp = NULL;done: if (filelist != NULL) __os_free(dbp->dbenv, filelist); (void)dbp->close(dbp, DB_NOSYNC); return (ret);}/* * __qam_exid -- * Generate a fileid for an extent based on the fileid of the main * file. Since we do not log schema creates/deletes explicitly, the log * never captures the fileid of an extent file. In order that masters and * replicas have the same fileids (so they can explicitly delete them), we * use computed fileids for the extent files of Queue files. * * An extent file id retains the low order 12 bytes of the file id and * overwrites the dev/inode fields, placing a 0 in the inode field, and * the extent number in the dev field. * * PUBLIC: void __qam_exid __P((DB *, u_int8_t *, u_int32_t)); */void__qam_exid(dbp, fidp, exnum) DB *dbp; u_int8_t *fidp; u_int32_t exnum;{ int i; u_int8_t *p; /* Copy the fileid from the master. */ memcpy(fidp, dbp->fileid, DB_FILE_ID_LEN); /* The first four bytes are the inode or the FileIndexLow; 0 it. */ for (i = sizeof(u_int32_t); i > 0; --i) *fidp++ = 0; /* The next four bytes are the dev/FileIndexHigh; insert the exnum . */ for (p = (u_int8_t *)&exnum, i = sizeof(u_int32_t); i > 0; --i) *fidp++ = *p++;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -