X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fscratch-writer.c;h=70a65ce9b67760ec585c9a54878174f4abc300bf;hb=707848060e414fe93458834446dd7cdbf800667f;hp=a2ce305e727b392e944a9609d816eb9483dffe84;hpb=43b1296aafe7582e7dbe6c2b6a8b478d7d9b0fcf;p=pspp-builds.git diff --git a/src/data/scratch-writer.c b/src/data/scratch-writer.c index a2ce305e..70a65ce9 100644 --- a/src/data/scratch-writer.c +++ b/src/data/scratch-writer.c @@ -21,12 +21,14 @@ #include #include +#include #include #include #include #include #include #include +#include #include #include @@ -35,9 +37,10 @@ /* A scratch file writer. */ struct scratch_writer { - struct scratch_handle *handle; /* Underlying scratch handle. */ struct file_handle *fh; /* Underlying file handle. */ - struct dict_compactor *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. */ }; @@ -51,45 +54,36 @@ 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 dict_compactor *compactor; struct casewriter *casewriter; + struct fh_lock *lock; + size_t dict_value_cnt; - if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "we")) + /* Get exclusive write access to handle. */ + lock = fh_lock (fh, FH_REF_SCRATCH, "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); - if (dict_compacting_would_shrink (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 = dict_make_compactor (scratch_dict); - dict_compact_values (scratch_dict); + writer->compactor = case_map_to_compact_dict (writer->dict, 0); + dict_compact_values (writer->dict); } else - compactor = NULL; - - /* Create new contents. */ - sh = xmalloc (sizeof *sh); - sh->dictionary = scratch_dict; - sh->casereader = NULL; + writer->compactor = NULL; + dict_value_cnt = dict_get_next_value_idx (writer->dict); + writer->subwriter = autopaging_writer_create (dict_value_cnt); - /* Create writer. */ - writer = xmalloc (sizeof *writer); - writer->handle = sh; - writer->fh = fh; - writer->compactor = compactor; - writer->subwriter = autopaging_writer_create (dict_get_next_value_idx ( - scratch_dict)); - - fh_set_scratch_handle (fh, sh); - casewriter = casewriter_create (&scratch_writer_casewriter_class, writer); + casewriter = casewriter_create (dict_value_cnt, + &scratch_writer_casewriter_class, writer); taint_propagate (casewriter_get_taint (writer->subwriter), casewriter_get_taint (casewriter)); return casewriter; @@ -101,12 +95,10 @@ scratch_writer_casewriter_write (struct casewriter *w UNUSED, void *writer_, struct ccase *c) { struct scratch_writer *writer = writer_; - struct scratch_handle *handle = writer->handle; struct ccase tmp; if (writer->compactor) { - case_create (&tmp, dict_get_next_value_idx (handle->dictionary)); - dict_compactor_compact (writer->compactor, &tmp, c); + case_map_execute (writer->compactor, c, &tmp); case_destroy (c); } else @@ -118,11 +110,32 @@ scratch_writer_casewriter_write (struct casewriter *w UNUSED, void *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); }