/*
- * $Id: store.cc,v 1.361 1998/01/01 00:05:53 wessels Exp $
+ * $Id: store.cc,v 1.362 1998/01/05 21:13:59 wessels Exp $
*
* DEBUG: section 20 Storeage Manager
* AUTHOR: Harvest Derived
*
* Writes a "clean" swap log file from in-memory metadata.
*/
+#define CLEAN_BUF_SZ 16384
int
storeWriteCleanLogs(int reopen)
{
char **cln;
int dirn;
dlink_node *m;
+ int linelen;
+ char **outbufs;
+ int *outbuflens;
if (store_rebuilding) {
debug(20, 1) ("Not currently OK to rewrite swap log.\n");
debug(20, 1) ("storeWriteCleanLogs: Operation aborted.\n");
fchmod(fd[dirn], sb.st_mode);
#endif
}
- line = xcalloc(1, 16384);
- for (m = all_list.head; m; m = m->next) {
+ line = xcalloc(1, CLEAN_BUF_SZ);
+ outbufs = xcalloc(Config.cacheSwap.n_configured, sizeof(char *));
+ outbuflens = xcalloc(Config.cacheSwap.n_configured, sizeof(int));
+ for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++) {
+ outbufs[dirn] = xcalloc(Config.cacheSwap.n_configured, CLEAN_BUF_SZ);
+ outbuflens[dirn] = 0;
+ }
+ for (m = all_list.tail; m; m = m->prev) {
e = m->data;
if (e->swap_file_number < 0)
continue;
assert(dirn < Config.cacheSwap.n_configured);
if (fd[dirn] < 0)
continue;
- snprintf(line, 16384, "%08x %08x %08x %08x %08x %9d %6d %08x %s\n",
+ snprintf(line, CLEAN_BUF_SZ, "%08x %08x %08x %08x %08x %9d %6d %08x %s\n",
(int) e->swap_file_number,
(int) e->timestamp,
(int) e->lastref,
e->refcount,
e->flag,
storeKeyText(e->key));
- if (write(fd[dirn], line, strlen(line)) < 0) {
- debug(50, 0) ("storeWriteCleanLogs: %s: %s\n", new[dirn], xstrerror());
- debug(20, 0) ("storeWriteCleanLogs: Current swap logfile not replaced.\n");
- file_close(fd[dirn]);
- fd[dirn] = -1;
- safeunlink(cln[dirn], 0);
- continue;
+ linelen = strlen(line);
+ /* buffered write */
+ if (linelen + outbuflens[dirn] > CLEAN_BUF_SZ-2) {
+ if (write(fd[dirn], outbufs[dirn], outbuflens[dirn]) < 0) {
+ debug(50, 0) ("storeWriteCleanLogs: %s: %s\n", new[dirn], xstrerror());
+ debug(20, 0) ("storeWriteCleanLogs: Current swap logfile not replaced.\n");
+ file_close(fd[dirn]);
+ fd[dirn] = -1;
+ safeunlink(cln[dirn], 0);
+ continue;
+ }
+ outbuflens[dirn] = 0;
}
+ strcpy(outbufs[dirn] + outbuflens[dirn], line);
+ outbuflens[dirn] += linelen;
if ((++n & 0x3FFF) == 0) {
getCurrentTime();
debug(20, 1) (" %7d lines written so far.\n", n);
}
}
safe_free(line);
+ /* flush */
+ for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++) {
+ if (outbuflens[dirn] > 0) {
+ if (write(fd[dirn], outbufs[dirn], outbuflens[dirn]) < 0) {
+ debug(50, 0) ("storeWriteCleanLogs: %s: %s\n", new[dirn], xstrerror());
+ debug(20, 0) ("storeWriteCleanLogs: Current swap logfile not replaced.\n");
+ file_close(fd[dirn]);
+ fd[dirn] = -1;
+ safeunlink(cln[dirn], 0);
+ continue;
+ }
+ }
+ safe_free(outbufs[dirn]);
+ }
+ safe_free(outbufs);
+ safe_free(outbuflens);
+ /* close */
for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++) {
file_close(fd[dirn]);
fd[dirn] = -1;
safe_free(fd);
return n;
}
+#undef CLEAN_BUF_SZ
int
storePendingNClients(const StoreEntry * e)