X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fscratch-writer.c;h=2c545c7d01a7520f2bc668759abbff19a495f5a1;hb=81579d9e9f994fb2908f50af41c3eb033d216e58;hp=4e2929a24164590e0440ddb42e460c46fa95c53f;hpb=9db3101d0bbbcfb687acd3e442e550557e4e56b1;p=pspp-builds.git diff --git a/src/data/scratch-writer.c b/src/data/scratch-writer.c index 4e2929a2..2c545c7d 100644 --- a/src/data/scratch-writer.c +++ b/src/data/scratch-writer.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2009, 2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -16,34 +16,37 @@ #include -#include "scratch-writer.h" +#include "data/scratch-writer.h" #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "data/case.h" +#include "data/case-map.h" +#include "data/casereader.h" +#include "data/casewriter-provider.h" +#include "data/casewriter.h" +#include "data/dictionary.h" +#include "data/file-handle-def.h" +#include "data/scratch-handle.h" +#include "data/variable.h" +#include "libpspp/compiler.h" +#include "libpspp/taint.h" -#include "xalloc.h" +#include "gl/xalloc.h" + +#define N_(msgid) (msgid) /* A scratch file writer. */ struct scratch_writer { - struct scratch_handle *handle; /* Underlying scratch handle. */ struct file_handle *fh; /* Underlying file handle. */ - struct case_map *compactor; /* Compacts into handle->dictionary. */ + struct fh_lock *lock; /* Exclusive access to file handle. */ + struct dictionary *dict; /* Dictionary for subwriter. */ + struct case_map *compactor; /* Compacts into dictionary. */ struct casewriter *subwriter; /* Data output. */ }; -static struct casewriter_class scratch_writer_casewriter_class; +static const struct casewriter_class scratch_writer_casewriter_class; /* Opens FH, which must have referent type FH_REF_SCRATCH, and returns a scratch_writer for it, or a null pointer on @@ -53,48 +56,35 @@ struct casewriter * scratch_writer_open (struct file_handle *fh, const struct dictionary *dictionary) { - struct scratch_handle *sh; struct scratch_writer *writer; - struct dictionary *scratch_dict; - struct case_map *compactor; struct casewriter *casewriter; - size_t dict_value_cnt; + struct fh_lock *lock; - if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "we")) + /* Get exclusive write access to handle. */ + /* TRANSLATORS: this fragment will be interpolated into + messages in fh_lock() that identify types of files. */ + lock = fh_lock (fh, FH_REF_SCRATCH, N_("scratch file"), FH_ACC_WRITE, true); + if (lock == NULL) return NULL; - /* Destroy previous contents of handle. */ - sh = fh_get_scratch_handle (fh); - if (sh != NULL) - scratch_handle_destroy (sh); + /* Create writer. */ + writer = xmalloc (sizeof *writer); + writer->lock = lock; + writer->fh = fh_ref (fh); - /* Copy the dictionary and compact if needed. */ - scratch_dict = dict_clone (dictionary); - dict_delete_scratch_vars (scratch_dict); - if (dict_count_values (scratch_dict, 0) - < dict_get_next_value_idx (scratch_dict)) + writer->dict = dict_clone (dictionary); + dict_delete_scratch_vars (writer->dict); + if (dict_count_values (writer->dict, 0) + < dict_get_next_value_idx (writer->dict)) { - compactor = case_map_to_compact_dict (scratch_dict, 0); - dict_compact_values (scratch_dict); + writer->compactor = case_map_to_compact_dict (writer->dict, 0); + dict_compact_values (writer->dict); } else - compactor = NULL; - dict_value_cnt = dict_get_next_value_idx (scratch_dict); + writer->compactor = NULL; + writer->subwriter = autopaging_writer_create (dict_get_proto (writer->dict)); - /* Create new contents. */ - sh = xmalloc (sizeof *sh); - sh->dictionary = scratch_dict; - sh->casereader = NULL; - - /* Create writer. */ - writer = xmalloc (sizeof *writer); - writer->handle = sh; - writer->fh = fh; - writer->compactor = compactor; - writer->subwriter = autopaging_writer_create (dict_value_cnt); - - fh_set_scratch_handle (fh, sh); - casewriter = casewriter_create (dict_value_cnt, + casewriter = casewriter_create (dict_get_proto (writer->dict), &scratch_writer_casewriter_class, writer); taint_propagate (casewriter_get_taint (writer->subwriter), casewriter_get_taint (casewriter)); @@ -107,30 +97,44 @@ scratch_writer_casewriter_write (struct casewriter *w UNUSED, void *writer_, struct ccase *c) { struct scratch_writer *writer = writer_; - struct ccase tmp; - if (writer->compactor) - { - case_map_execute (writer->compactor, c, &tmp); - case_destroy (c); - } - else - case_move (&tmp, c); - casewriter_write (writer->subwriter, &tmp); + casewriter_write (writer->subwriter, + case_map_execute (writer->compactor, c)); } /* Closes WRITER. */ static void scratch_writer_casewriter_destroy (struct casewriter *w UNUSED, void *writer_) { + static unsigned int next_unique_id = 0x12345678; + struct scratch_writer *writer = writer_; struct casereader *reader = casewriter_make_reader (writer->subwriter); if (!casereader_error (reader)) - writer->handle->casereader = reader; - fh_close (writer->fh, "scratch file", "we"); + { + /* Destroy previous contents of handle. */ + struct scratch_handle *sh = fh_get_scratch_handle (writer->fh); + if (sh != NULL) + scratch_handle_destroy (sh); + + /* Create new contents. */ + sh = xmalloc (sizeof *sh); + sh->unique_id = ++next_unique_id; + sh->dictionary = writer->dict; + sh->casereader = reader; + fh_set_scratch_handle (writer->fh, sh); + } + else + { + casereader_destroy (reader); + dict_destroy (writer->dict); + } + + fh_unlock (writer->lock); + fh_unref (writer->fh); free (writer); } -static struct casewriter_class scratch_writer_casewriter_class = +static const struct casewriter_class scratch_writer_casewriter_class = { scratch_writer_casewriter_write, scratch_writer_casewriter_destroy,