-/* PSPP - computes sample statistics.
- Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
- Written by Ben Pfaff <blp@gnu.org>.
+/* PSPP - a program for statistical analysis.
+ Copyright (C) 1997-9, 2000, 2006, 2007 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 the Free Software Foundation; either version 2 of the
- License, or (at your option) any later version.
+ 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
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301, USA. */
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <stdlib.h>
#include <unistd.h>
-#include <data/case-source.h>
-#include <data/case-sink.h>
#include <data/case.h>
-#include <data/casefile.h>
+#include <data/caseinit.h>
+#include <data/casereader.h>
+#include <data/casereader-provider.h>
+#include <data/casewriter.h>
#include <data/dictionary.h>
#include <data/file-handle-def.h>
#include <data/procedure.h>
-#include <data/settings.h>
-#include <data/storage-stream.h>
#include <data/transformations.h>
-#include <data/value-labels.h>
#include <data/variable.h>
-#include <language/expressions/public.h>
#include <libpspp/alloc.h>
-#include <libpspp/message.h>
+#include <libpspp/deque.h>
#include <libpspp/misc.h>
#include <libpspp/str.h>
-#include <output/manager.h>
-#include <output/table.h>
-
-#include "gettext.h"
-#define _(msgid) gettext (msgid)
+#include <libpspp/taint.h>
+
+struct dataset {
+ /* Cases are read from source,
+ their transformation variables are initialized,
+ pass through permanent_trns_chain (which transforms them into
+ the format described by permanent_dict),
+ are written to sink,
+ pass through temporary_trns_chain (which transforms them into
+ the format described by dict),
+ and are finally passed to the procedure. */
+ struct casereader *source;
+ struct caseinit *caseinit;
+ struct trns_chain *permanent_trns_chain;
+ struct dictionary *permanent_dict;
+ struct casewriter *sink;
+ struct trns_chain *temporary_trns_chain;
+ struct dictionary *dict;
+
+ /* Callback which occurs when a procedure provides a new source for
+ the dataset */
+ replace_source_callback *replace_source ;
+
+ /* Callback which occurs whenever the DICT is replaced by a new one */
+ replace_dictionary_callback *replace_dict;
+
+ /* If true, cases are discarded instead of being written to
+ sink. */
+ bool discard_output;
+
+ /* The transformation chain that the next transformation will be
+ added to. */
+ struct trns_chain *cur_trns_chain;
+
+ /* The compactor used to compact a case, if necessary;
+ otherwise a null pointer. */
+ struct dict_compactor *compactor;
+
+ /* Time at which proc was last invoked. */
+ time_t last_proc_invocation;
+
+ /* Cases just before ("lagging") the current one. */
+ int n_lag; /* Number of cases to lag. */
+ struct deque lag; /* Deque of lagged cases. */
+ struct ccase *lag_cases; /* Lagged cases managed by deque. */
+
+ /* Procedure data. */
+ enum
+ {
+ PROC_COMMITTED, /* No procedure in progress. */
+ PROC_OPEN, /* proc_open called, casereader still open. */
+ PROC_CLOSED /* casereader from proc_open destroyed,
+ but proc_commit not yet called. */
+ }
+ proc_state;
+ casenumber cases_written; /* Cases output so far. */
+ bool ok; /* Error status. */
+}; /* struct dataset */
-/*
- Virtual File Manager (vfm):
- vfm is used to process data files. It uses the model that
- data is read from one stream (the data source), processed,
- then written to another (the data sink). The data source is
- then deleted and the data sink becomes the data source for the
- next procedure. */
+static void add_case_limit_trns (struct dataset *ds);
+static void add_filter_trns (struct dataset *ds);
-/* Procedure execution data. */
-struct write_case_data
- {
- /* Function to call for each case. */
- bool (*proc_func) (struct ccase *, void *); /* Function. */
- void *aux; /* Auxiliary data. */
-
- struct ccase trns_case; /* Case used for transformations. */
- struct ccase sink_case; /* Case written to sink, if
- compaction is necessary. */
- size_t cases_written; /* Cases output so far. */
- size_t cases_analyzed; /* Cases passed to procedure so far. */
- };
-
-/* Cases are read from vfm_source,
- pass through permanent_trns_chain (which transforms them into
- the format described by permanent_dict),
- are written to vfm_sink,
- pass through temporary_trns_chain (which transforms them into
- the format described by default_dict),
- and are finally passed to the procedure. */
-static struct case_source *vfm_source;
-static struct trns_chain *permanent_trns_chain;
-static struct dictionary *permanent_dict;
-static struct case_sink *vfm_sink;
-static struct trns_chain *temporary_trns_chain;
-struct dictionary *default_dict;
-
-/* The transformation chain that the next transformation will be
- added to. */
-static struct trns_chain *cur_trns_chain;
-
-/* The compactor used to compact a case, if necessary;
- otherwise a null pointer. */
-static struct dict_compactor *compactor;
-
-/* Time at which vfm was last invoked. */
-static time_t last_vfm_invocation;
-
-/* Lag queue. */
-int n_lag; /* Number of cases to lag. */
-static int lag_count; /* Number of cases in lag_queue so far. */
-static int lag_head; /* Index where next case will be added. */
-static struct ccase *lag_queue; /* Array of n_lag ccase * elements. */
-
-static void add_case_limit_trns (void);
-static void add_filter_trns (void);
-static void add_process_if_trns (void);
-
-static bool internal_procedure (bool (*proc_func) (struct ccase *, void *),
- void *aux);
-static void update_last_vfm_invocation (void);
-static void create_trns_case (struct ccase *, struct dictionary *);
-static void open_active_file (void);
-static bool write_case (struct write_case_data *wc_data);
-static void lag_case (const struct ccase *c);
-static void clear_case (struct ccase *c);
-static bool close_active_file (void);
+static void update_last_proc_invocation (struct dataset *ds);
\f
/* Public functions. */
/* Returns the last time the data was read. */
time_t
-time_of_last_procedure (void)
+time_of_last_procedure (struct dataset *ds)
{
- if (last_vfm_invocation == 0)
- update_last_vfm_invocation ();
- return last_vfm_invocation;
+ if (ds->last_proc_invocation == 0)
+ update_last_proc_invocation (ds);
+ return ds->last_proc_invocation;
}
+\f
+/* Regular procedure. */
-/* Reads the data from the input program and writes it to a new
- active file. For each case we read from the input program, we
- do the following:
-
- 1. Execute permanent transformations. If these drop the case,
- start the next case from step 1.
-
- 2. Write case to replacement active file.
-
- 3. Execute temporary transformations. If these drop the case,
- start the next case from step 1.
-
- 4. Pass case to PROC_FUNC, passing AUX as auxiliary data.
-
- Returns true if successful, false if an I/O error occurred. */
+/* Executes any pending transformations, if necessary.
+ This is not identical to the EXECUTE command in that it won't
+ always read the source data. This can be important when the
+ source data is given inline within BEGIN DATA...END FILE. */
bool
-procedure (bool (*proc_func) (struct ccase *, void *), void *aux)
+proc_execute (struct dataset *ds)
{
- if (proc_func == NULL
- && case_source_is_class (vfm_source, &storage_source_class)
- && vfm_sink == NULL
- && temporary_trns_chain == NULL
- && trns_chain_is_empty (permanent_trns_chain))
- {
- expr_free (process_if_expr);
- process_if_expr = NULL;
- dict_set_case_limit (default_dict, 0);
- dict_clear_vectors (default_dict);
-
- update_last_vfm_invocation ();
- return true;
- }
- else
- {
- bool ok;
-
- open_active_file ();
- ok = internal_procedure (proc_func, aux);
- ok = close_active_file () && ok;
-
- return ok;
- }
-}
-
-/* Callback function for multipass_procedure(). */
-static bool
-multipass_callback (struct ccase *c, void *cf_)
-{
- struct casefile *cf = cf_;
- return casefile_append (cf, c);
-}
+ bool ok;
-/* Procedure that allows multiple passes over the input data.
- The entire active file is passed to PROC_FUNC, with the given
- AUX as auxiliary data, as a unit. */
-bool
-multipass_procedure (bool (*proc_func) (const struct casefile *, void *aux),
- void *aux)
-{
- if (case_source_is_class (vfm_source, &storage_source_class)
- && vfm_sink == NULL
- && temporary_trns_chain == NULL
- && trns_chain_is_empty (permanent_trns_chain))
+ if ((ds->temporary_trns_chain == NULL
+ || trns_chain_is_empty (ds->temporary_trns_chain))
+ && trns_chain_is_empty (ds->permanent_trns_chain))
{
- proc_func (storage_source_get_casefile (vfm_source), aux);
-
- expr_free (process_if_expr);
- process_if_expr = NULL;
- dict_set_case_limit (default_dict, 0);
- dict_clear_vectors (default_dict);
-
- update_last_vfm_invocation ();
+ ds->n_lag = 0;
+ ds->discard_output = false;
+ dict_set_case_limit (ds->dict, 0);
+ dict_clear_vectors (ds->dict);
return true;
}
- else
- {
- struct casefile *cf;
- bool ok;
-
- assert (proc_func != NULL);
-
- cf = casefile_create (dict_get_next_value_idx (default_dict));
-
- open_active_file ();
- ok = internal_procedure (multipass_callback, cf);
- ok = proc_func (cf, aux) && ok;
- ok = close_active_file () && ok;
- casefile_destroy (cf);
-
- return ok;
- }
+ ok = casereader_destroy (proc_open (ds));
+ return proc_commit (ds) && ok;
}
-/* Executes a procedure, as procedure(), except that the caller
- is responsible for calling open_active_file() and
- close_active_file().
- Returns true if successful, false if an I/O error occurred. */
-static bool
-internal_procedure (bool (*proc_func) (struct ccase *, void *), void *aux)
-{
- struct write_case_data wc_data;
- bool ok;
-
- wc_data.proc_func = proc_func;
- wc_data.aux = aux;
- create_trns_case (&wc_data.trns_case, default_dict);
- case_create (&wc_data.sink_case, dict_get_next_value_idx (default_dict));
- wc_data.cases_written = 0;
-
- update_last_vfm_invocation ();
+static struct casereader_class proc_casereader_class;
- ok = (vfm_source == NULL
- || vfm_source->class->read (vfm_source,
- &wc_data.trns_case,
- write_case, &wc_data));
-
- case_destroy (&wc_data.sink_case);
- case_destroy (&wc_data.trns_case);
-
- return ok;
-}
-
-/* Updates last_vfm_invocation. */
-static void
-update_last_vfm_invocation (void)
+/* Opens dataset DS for reading cases with proc_read.
+ proc_commit must be called when done. */
+struct casereader *
+proc_open (struct dataset *ds)
{
- last_vfm_invocation = time (NULL);
-}
+ assert (ds->source != NULL);
+ assert (ds->proc_state == PROC_COMMITTED);
-/* Creates and returns a case, initializing it from the vectors
- that say which `value's need to be initialized just once, and
- which ones need to be re-initialized before every case. */
-static void
-create_trns_case (struct ccase *trns_case, struct dictionary *dict)
-{
- size_t var_cnt = dict_get_var_cnt (dict);
- size_t i;
+ update_last_proc_invocation (ds);
- case_create (trns_case, dict_get_next_value_idx (dict));
- for (i = 0; i < var_cnt; i++)
- {
- struct variable *v = dict_get_var (dict, i);
- union value *value = case_data_rw (trns_case, v->fv);
-
- if (v->type == NUMERIC)
- value->f = v->leave ? 0.0 : SYSMIS;
- else
- memset (value->s, ' ', v->width);
- }
-}
-
-/* Makes all preparations for reading from the data source and writing
- to the data sink. */
-static void
-open_active_file (void)
-{
- add_case_limit_trns ();
- add_filter_trns ();
- add_process_if_trns ();
+ caseinit_mark_for_init (ds->caseinit, ds->dict);
- /* Finalize transformations. */
- trns_chain_finalize (cur_trns_chain);
+ /* Finish up the collection of transformations. */
+ add_case_limit_trns (ds);
+ add_filter_trns (ds);
+ trns_chain_finalize (ds->cur_trns_chain);
/* Make permanent_dict refer to the dictionary right before
data reaches the sink. */
- if (permanent_dict == NULL)
- permanent_dict = default_dict;
-
- /* Figure out compaction. */
- compactor = (dict_needs_compaction (permanent_dict)
- ? dict_make_compactor (permanent_dict)
- : NULL);
+ if (ds->permanent_dict == NULL)
+ ds->permanent_dict = ds->dict;
/* Prepare sink. */
- if (vfm_sink == NULL)
- vfm_sink = create_case_sink (&storage_sink_class, permanent_dict, NULL);
- if (vfm_sink->class->open != NULL)
- vfm_sink->class->open (vfm_sink);
-
- /* Allocate memory for lag queue. */
- if (n_lag > 0)
- {
- int i;
-
- lag_count = 0;
- lag_head = 0;
- lag_queue = xnmalloc (n_lag, sizeof *lag_queue);
- for (i = 0; i < n_lag; i++)
- case_nullify (&lag_queue[i]);
- }
-}
-
-/* Transforms trns_case and writes it to the replacement active
- file if advisable. Returns true if more cases can be
- accepted, false otherwise. Do not call this function again
- after it has returned false once. */
-static bool
-write_case (struct write_case_data *wc_data)
-{
- enum trns_result retval;
- size_t case_nr;
-
- /* Execute permanent transformations. */
- case_nr = wc_data->cases_written + 1;
- retval = trns_chain_execute (permanent_trns_chain,
- &wc_data->trns_case, &case_nr);
- if (retval != TRNS_CONTINUE)
- goto done;
-
- /* Write case to LAG queue. */
- if (n_lag)
- lag_case (&wc_data->trns_case);
-
- /* Write case to replacement active file. */
- wc_data->cases_written++;
- if (vfm_sink->class->write != NULL)
+ if (!ds->discard_output)
{
- if (compactor != NULL)
- {
- dict_compactor_compact (compactor, &wc_data->sink_case,
- &wc_data->trns_case);
- vfm_sink->class->write (vfm_sink, &wc_data->sink_case);
- }
- else
- vfm_sink->class->write (vfm_sink, &wc_data->trns_case);
+ ds->compactor = (dict_compacting_would_shrink (ds->permanent_dict)
+ ? dict_make_compactor (ds->permanent_dict)
+ : NULL);
+ ds->sink = autopaging_writer_create (dict_get_compacted_value_cnt (
+ ds->permanent_dict));
}
-
- /* Execute temporary transformations. */
- if (temporary_trns_chain != NULL)
+ else
{
- retval = trns_chain_execute (temporary_trns_chain,
- &wc_data->trns_case,
- &wc_data->cases_written);
- if (retval != TRNS_CONTINUE)
- goto done;
+ ds->compactor = NULL;
+ ds->sink = NULL;
}
- /* Pass case to procedure. */
- wc_data->cases_analyzed++;
- if (wc_data->proc_func != NULL)
- if (!wc_data->proc_func (&wc_data->trns_case, wc_data->aux))
- retval = TRNS_ERROR;
+ /* Allocate memory for lagged cases. */
+ ds->lag_cases = deque_init (&ds->lag, ds->n_lag, sizeof *ds->lag_cases);
+
+ ds->proc_state = PROC_OPEN;
+ ds->cases_written = 0;
+ ds->ok = true;
- done:
- clear_case (&wc_data->trns_case);
- return retval != TRNS_ERROR;
+ /* FIXME: use taint in dataset in place of `ok'? */
+ /* FIXME: for trivial cases we can just return a clone of
+ ds->source? */
+ return casereader_create_sequential (NULL,
+ dict_get_next_value_idx (ds->dict),
+ CASENUMBER_MAX,
+ &proc_casereader_class, ds);
}
-/* Add C to the lag queue. */
-static void
-lag_case (const struct ccase *c)
+/* Returns true if a procedure is in progress, that is, if
+ proc_open has been called but proc_commit has not. */
+bool
+proc_is_open (const struct dataset *ds)
{
- if (lag_count < n_lag)
- lag_count++;
- case_destroy (&lag_queue[lag_head]);
- case_clone (&lag_queue[lag_head], c);
- if (++lag_head >= n_lag)
- lag_head = 0;
+ return ds->proc_state != PROC_COMMITTED;
}
-/* Clears the variables in C that need to be cleared between
- processing cases. */
-static void
-clear_case (struct ccase *c)
+/* "read" function for procedure casereader. */
+static bool
+proc_casereader_read (struct casereader *reader UNUSED, void *ds_,
+ struct ccase *c)
{
- size_t var_cnt = dict_get_var_cnt (default_dict);
- size_t i;
-
- for (i = 0; i < var_cnt; i++)
+ struct dataset *ds = ds_;
+ enum trns_result retval = TRNS_DROP_CASE;
+
+ assert (ds->proc_state == PROC_OPEN);
+ for (;;)
{
- struct variable *v = dict_get_var (default_dict, i);
- if (!v->leave)
+ casenumber case_nr;
+
+ assert (retval == TRNS_DROP_CASE || retval == TRNS_ERROR);
+ if (retval == TRNS_ERROR)
+ ds->ok = false;
+ if (!ds->ok)
+ return false;
+
+ /* Read a case from source. */
+ if (!casereader_read (ds->source, c))
+ return false;
+ case_resize (c, dict_get_next_value_idx (ds->dict));
+ caseinit_init_vars (ds->caseinit, c);
+
+ /* Execute permanent transformations. */
+ case_nr = ds->cases_written + 1;
+ retval = trns_chain_execute (ds->permanent_trns_chain, TRNS_CONTINUE,
+ c, case_nr);
+ caseinit_update_left_vars (ds->caseinit, c);
+ if (retval != TRNS_CONTINUE)
{
- if (v->type == NUMERIC)
- case_data_rw (c, v->fv)->f = SYSMIS;
+ case_destroy (c);
+ continue;
+ }
+
+ /* Write case to collection of lagged cases. */
+ if (ds->n_lag > 0)
+ {
+ while (deque_count (&ds->lag) >= ds->n_lag)
+ case_destroy (&ds->lag_cases[deque_pop_back (&ds->lag)]);
+ case_clone (&ds->lag_cases[deque_push_front (&ds->lag)], c);
+ }
+
+ /* Write case to replacement active file. */
+ ds->cases_written++;
+ if (ds->sink != NULL)
+ {
+ struct ccase tmp;
+ if (ds->compactor != NULL)
+ {
+ case_create (&tmp, dict_get_compacted_value_cnt (ds->dict));
+ dict_compactor_compact (ds->compactor, &tmp, c);
+ }
else
- memset (case_data_rw (c, v->fv)->s, ' ', v->width);
- }
- }
-}
+ case_clone (&tmp, c);
+ casewriter_write (ds->sink, &tmp);
+ }
-/* Closes the active file. */
-static bool
-close_active_file (void)
-{
- /* Free memory for lag queue, and turn off lagging. */
- if (n_lag > 0)
- {
- int i;
-
- for (i = 0; i < n_lag; i++)
- case_destroy (&lag_queue[i]);
- free (lag_queue);
- n_lag = 0;
- }
-
- /* Dictionary from before TEMPORARY becomes permanent. */
- proc_cancel_temporary_transformations ();
+ /* Execute temporary transformations. */
+ if (ds->temporary_trns_chain != NULL)
+ {
+ retval = trns_chain_execute (ds->temporary_trns_chain, TRNS_CONTINUE,
+ c, ds->cases_written);
+ if (retval != TRNS_CONTINUE)
+ {
+ case_destroy (c);
+ continue;
+ }
+ }
- /* Finish compaction. */
- if (compactor != NULL)
- {
- dict_compactor_destroy (compactor);
- dict_compact_values (default_dict);
+ return true;
}
-
- /* Free data source. */
- free_case_source (vfm_source);
- vfm_source = NULL;
-
- /* Old data sink becomes new data source. */
- if (vfm_sink->class->make_source != NULL)
- vfm_source = vfm_sink->class->make_source (vfm_sink);
- free_case_sink (vfm_sink);
- vfm_sink = NULL;
-
- /* Cancel TEMPORARY, PROCESS IF, FILTER, N OF CASES, vectors,
- and get rid of all the transformations. */
- dict_clear_vectors (default_dict);
- permanent_dict = NULL;
- return proc_cancel_all_transformations ();
}
-\f
-/* Returns a pointer to the lagged case from N_BEFORE cases before the
- current one, or NULL if there haven't been that many cases yet. */
-struct ccase *
-lagged_case (int n_before)
+
+/* "destroy" function for procedure casereader. */
+static void
+proc_casereader_destroy (struct casereader *reader, void *ds_)
{
- assert (n_before >= 1 );
- assert (n_before <= n_lag);
+ struct dataset *ds = ds_;
+ struct ccase c;
- if (n_before <= lag_count)
- {
- int index = lag_head - n_before;
- if (index < 0)
- index += n_lag;
- return &lag_queue[index];
- }
- else
- return NULL;
+ /* Make sure transformations happen for every input case, in
+ case they have side effects, and ensure that the replacement
+ active file gets all the cases it should. */
+ while (casereader_read (reader, &c))
+ case_destroy (&c);
+
+ ds->proc_state = PROC_CLOSED;
+ ds->ok = casereader_destroy (ds->source) && ds->ok;
+ ds->source = NULL;
+ proc_set_active_file_data (ds, NULL);
}
-\f
-/* Represents auxiliary data for handling SPLIT FILE. */
-struct split_aux_data
- {
- size_t case_count; /* Number of cases so far. */
- struct ccase prev_case; /* Data in previous case. */
-
- /* Functions to call... */
- void (*begin_func) (void *); /* ...before data. */
- bool (*proc_func) (struct ccase *, void *); /* ...with data. */
- void (*end_func) (void *); /* ...after data. */
- void *func_aux; /* Auxiliary data. */
- };
-static int equal_splits (const struct ccase *, const struct ccase *);
-static bool procedure_with_splits_callback (struct ccase *, void *);
-static void dump_splits (struct ccase *);
-
-/* Like procedure(), but it automatically breaks the case stream
- into SPLIT FILE break groups. Before each group of cases with
- identical SPLIT FILE variable values, BEGIN_FUNC is called.
- Then PROC_FUNC is called with each case in the group.
- END_FUNC is called when the group is finished. FUNC_AUX is
- passed to each of the functions as auxiliary data.
-
- If the active file is empty, none of BEGIN_FUNC, PROC_FUNC,
- and END_FUNC will be called at all.
-
- If SPLIT FILE is not in effect, then there is one break group
- (if the active file is nonempty), and BEGIN_FUNC and END_FUNC
- will be called once.
-
- Returns true if successful, false if an I/O error occurred. */
+/* Must return false if the source casereader, a transformation,
+ or the sink casewriter signaled an error. (If a temporary
+ transformation signals an error, then the return value is
+ false, but the replacement active file may still be
+ untainted.) */
bool
-procedure_with_splits (void (*begin_func) (void *aux),
- bool (*proc_func) (struct ccase *, void *aux),
- void (*end_func) (void *aux),
- void *func_aux)
+proc_commit (struct dataset *ds)
{
- struct split_aux_data split_aux;
- bool ok;
-
- split_aux.case_count = 0;
- case_nullify (&split_aux.prev_case);
- split_aux.begin_func = begin_func;
- split_aux.proc_func = proc_func;
- split_aux.end_func = end_func;
- split_aux.func_aux = func_aux;
+ assert (ds->proc_state == PROC_CLOSED);
+ ds->proc_state = PROC_COMMITTED;
- open_active_file ();
- ok = internal_procedure (procedure_with_splits_callback, &split_aux);
- if (split_aux.case_count > 0 && end_func != NULL)
- end_func (func_aux);
- if (!close_active_file ())
- ok = false;
+ /* Free memory for lagged cases. */
+ while (!deque_is_empty (&ds->lag))
+ case_destroy (&ds->lag_cases[deque_pop_back (&ds->lag)]);
+ free (ds->lag_cases);
- case_destroy (&split_aux.prev_case);
-
- return ok;
-}
-
-/* procedure() callback used by procedure_with_splits(). */
-static bool
-procedure_with_splits_callback (struct ccase *c, void *split_aux_)
-{
- struct split_aux_data *split_aux = split_aux_;
+ /* Dictionary from before TEMPORARY becomes permanent. */
+ proc_cancel_temporary_transformations (ds);
- /* Start a new series if needed. */
- if (split_aux->case_count == 0
- || !equal_splits (c, &split_aux->prev_case))
+ if (!ds->discard_output)
{
- if (split_aux->case_count > 0 && split_aux->end_func != NULL)
- split_aux->end_func (split_aux->func_aux);
-
- dump_splits (c);
- case_destroy (&split_aux->prev_case);
- case_clone (&split_aux->prev_case, c);
+ /* Finish compacting. */
+ if (ds->compactor != NULL)
+ {
+ dict_compactor_destroy (ds->compactor);
+ dict_compact_values (ds->dict);
+ ds->compactor = NULL;
+ }
- if (split_aux->begin_func != NULL)
- split_aux->begin_func (split_aux->func_aux);
+ /* Old data sink becomes new data source. */
+ if (ds->sink != NULL)
+ ds->source = casewriter_make_reader (ds->sink);
}
-
- split_aux->case_count++;
- if (split_aux->proc_func != NULL)
- return split_aux->proc_func (c, split_aux->func_aux);
else
- return true;
-}
-
-/* Compares the SPLIT FILE variables in cases A and B and returns
- nonzero only if they differ. */
-static int
-equal_splits (const struct ccase *a, const struct ccase *b)
-{
- return case_compare (a, b,
- dict_get_split_vars (default_dict),
- dict_get_split_cnt (default_dict)) == 0;
-}
-
-/* Dumps out the values of all the split variables for the case C. */
-static void
-dump_splits (struct ccase *c)
-{
- struct variable *const *split;
- struct tab_table *t;
- size_t split_cnt;
- int i;
-
- split_cnt = dict_get_split_cnt (default_dict);
- if (split_cnt == 0)
- return;
-
- t = tab_create (3, split_cnt + 1, 0);
- tab_dim (t, tab_natural_dimensions);
- tab_vline (t, TAL_GAP, 1, 0, split_cnt);
- tab_vline (t, TAL_GAP, 2, 0, split_cnt);
- tab_text (t, 0, 0, TAB_NONE, _("Variable"));
- tab_text (t, 1, 0, TAB_LEFT, _("Value"));
- tab_text (t, 2, 0, TAB_LEFT, _("Label"));
- split = dict_get_split_vars (default_dict);
- for (i = 0; i < split_cnt; i++)
{
- struct variable *v = split[i];
- char temp_buf[80];
- const char *val_lab;
-
- assert (v->type == NUMERIC || v->type == ALPHA);
- tab_text (t, 0, i + 1, TAB_LEFT | TAT_PRINTF, "%s", v->name);
-
- data_out (temp_buf, &v->print, case_data (c, v->fv));
-
- temp_buf[v->print.w] = 0;
- tab_text (t, 1, i + 1, TAT_PRINTF, "%.*s", v->print.w, temp_buf);
-
- val_lab = val_labs_find (v->val_labs, *case_data (c, v->fv));
- if (val_lab)
- tab_text (t, 2, i + 1, TAB_LEFT, val_lab);
+ ds->source = NULL;
+ ds->discard_output = false;
}
- tab_flags (t, SOMF_NO_TITLE);
- tab_submit (t);
-}
-\f
-/* Represents auxiliary data for handling SPLIT FILE in a
- multipass procedure. */
-struct multipass_split_aux_data
- {
- struct ccase prev_case; /* Data in previous case. */
- struct casefile *casefile; /* Accumulates data for a split. */
-
- /* Function to call with the accumulated data. */
- bool (*split_func) (const struct casefile *, void *);
- void *func_aux; /* Auxiliary data. */
- };
-
-static bool multipass_split_callback (struct ccase *c, void *aux_);
-static bool multipass_split_output (struct multipass_split_aux_data *);
-
-/* Returns true if successful, false if an I/O error occurred. */
-bool
-multipass_procedure_with_splits (bool (*split_func) (const struct casefile *,
- void *),
- void *func_aux)
-{
- struct multipass_split_aux_data aux;
- bool ok;
-
- assert (split_func != NULL);
-
- open_active_file ();
-
- case_nullify (&aux.prev_case);
- aux.casefile = NULL;
- aux.split_func = split_func;
- aux.func_aux = func_aux;
+ ds->sink = NULL;
+ if ( ds->replace_source) ds->replace_source (ds->source);
- ok = internal_procedure (multipass_split_callback, &aux);
- if (aux.casefile != NULL)
- ok = multipass_split_output (&aux) && ok;
- case_destroy (&aux.prev_case);
+ caseinit_clear (ds->caseinit);
+ caseinit_mark_as_preinited (ds->caseinit, ds->dict);
- if (!close_active_file ())
- ok = false;
-
- return ok;
+ dict_clear_vectors (ds->dict);
+ ds->permanent_dict = NULL;
+ return proc_cancel_all_transformations (ds) && ds->ok;
}
-/* procedure() callback used by multipass_procedure_with_splits(). */
-static bool
-multipass_split_callback (struct ccase *c, void *aux_)
-{
- struct multipass_split_aux_data *aux = aux_;
- bool ok = true;
-
- /* Start a new series if needed. */
- if (aux->casefile == NULL || !equal_splits (c, &aux->prev_case))
- {
- /* Pass any cases to split_func. */
- if (aux->casefile != NULL)
- ok = multipass_split_output (aux);
-
- /* Start a new casefile. */
- aux->casefile = casefile_create (dict_get_next_value_idx (default_dict));
-
- /* Record split values. */
- dump_splits (c);
- case_destroy (&aux->prev_case);
- case_clone (&aux->prev_case, c);
- }
-
- return casefile_append (aux->casefile, c) && ok;
-}
+/* Casereader class for procedure execution. */
+static struct casereader_class proc_casereader_class =
+ {
+ proc_casereader_read,
+ proc_casereader_destroy,
+ NULL,
+ NULL,
+ };
-static bool
-multipass_split_output (struct multipass_split_aux_data *aux)
+/* Updates last_proc_invocation. */
+static void
+update_last_proc_invocation (struct dataset *ds)
{
- bool ok;
-
- assert (aux->casefile != NULL);
- ok = aux->split_func (aux->casefile, aux->func_aux);
- casefile_destroy (aux->casefile);
- aux->casefile = NULL;
-
- return ok;
+ ds->last_proc_invocation = time (NULL);
}
-
-/* Discards all the current state in preparation for a data-input
- command like DATA LIST or GET. */
-void
-discard_variables (void)
+\f
+/* Returns a pointer to the lagged case from N_BEFORE cases before the
+ current one, or NULL if there haven't been that many cases yet. */
+struct ccase *
+lagged_case (const struct dataset *ds, int n_before)
{
- dict_clear (default_dict);
- fh_set_default_handle (NULL);
-
- n_lag = 0;
-
- if (vfm_source != NULL)
- {
- free_case_source (vfm_source);
- vfm_source = NULL;
- }
-
- proc_cancel_all_transformations ();
-
- expr_free (process_if_expr);
- process_if_expr = NULL;
+ assert (n_before >= 1);
+ assert (n_before <= ds->n_lag);
- proc_cancel_temporary_transformations ();
+ if (n_before <= deque_count (&ds->lag))
+ return &ds->lag_cases[deque_front (&ds->lag, n_before - 1)];
+ else
+ return NULL;
}
\f
/* Returns the current set of permanent transformations,
and clears the permanent transformations.
For use by INPUT PROGRAM. */
struct trns_chain *
-proc_capture_transformations (void)
+proc_capture_transformations (struct dataset *ds)
{
struct trns_chain *chain;
-
- assert (temporary_trns_chain == NULL);
- chain = permanent_trns_chain;
- cur_trns_chain = permanent_trns_chain = trns_chain_create ();
+
+ assert (ds->temporary_trns_chain == NULL);
+ chain = ds->permanent_trns_chain;
+ ds->cur_trns_chain = ds->permanent_trns_chain = trns_chain_create ();
return chain;
}
frees itself with FREE to the current set of transformations.
The functions are passed AUX as auxiliary data. */
void
-add_transformation (trns_proc_func *proc, trns_free_func *free, void *aux)
+add_transformation (struct dataset *ds, trns_proc_func *proc, trns_free_func *free, void *aux)
{
- trns_chain_append (cur_trns_chain, NULL, proc, free, aux);
+ trns_chain_append (ds->cur_trns_chain, NULL, proc, free, aux);
}
/* Adds a transformation that processes a case with PROC and
FINALIZE will be called.
The functions are passed AUX as auxiliary data. */
void
-add_transformation_with_finalizer (trns_finalize_func *finalize,
+add_transformation_with_finalizer (struct dataset *ds,
+ trns_finalize_func *finalize,
trns_proc_func *proc,
trns_free_func *free, void *aux)
{
- trns_chain_append (cur_trns_chain, finalize, proc, free, aux);
+ trns_chain_append (ds->cur_trns_chain, finalize, proc, free, aux);
}
/* Returns the index of the next transformation.
This value can be returned by a transformation procedure
function to indicate a "jump" to that transformation. */
size_t
-next_transformation (void)
+next_transformation (const struct dataset *ds)
{
- return trns_chain_next (cur_trns_chain);
+ return trns_chain_next (ds->cur_trns_chain);
}
/* Returns true if the next call to add_transformation() will add
a temporary transformation, false if it will add a permanent
transformation. */
bool
-proc_in_temporary_transformations (void)
+proc_in_temporary_transformations (const struct dataset *ds)
{
- return temporary_trns_chain != NULL;
+ return ds->temporary_trns_chain != NULL;
}
/* Marks the start of temporary transformations.
Further calls to add_transformation() will add temporary
transformations. */
void
-proc_start_temporary_transformations (void)
+proc_start_temporary_transformations (struct dataset *ds)
{
- if (!proc_in_temporary_transformations ())
+ if (!proc_in_temporary_transformations (ds))
{
- add_case_limit_trns ();
+ add_case_limit_trns (ds);
+
+ ds->permanent_dict = dict_clone (ds->dict);
- permanent_dict = dict_clone (default_dict);
- trns_chain_finalize (permanent_trns_chain);
- temporary_trns_chain = cur_trns_chain = trns_chain_create ();
+ trns_chain_finalize (ds->permanent_trns_chain);
+ ds->temporary_trns_chain = ds->cur_trns_chain = trns_chain_create ();
}
}
permanent.
Returns true if anything changed, false otherwise. */
bool
-proc_make_temporary_transformations_permanent (void)
+proc_make_temporary_transformations_permanent (struct dataset *ds)
{
- if (proc_in_temporary_transformations ())
+ if (proc_in_temporary_transformations (ds))
{
- trns_chain_finalize (temporary_trns_chain);
- trns_chain_splice (permanent_trns_chain, temporary_trns_chain);
- temporary_trns_chain = NULL;
+ trns_chain_finalize (ds->temporary_trns_chain);
+ trns_chain_splice (ds->permanent_trns_chain, ds->temporary_trns_chain);
+ ds->temporary_trns_chain = NULL;
- dict_destroy (permanent_dict);
- permanent_dict = NULL;
+ dict_destroy (ds->permanent_dict);
+ ds->permanent_dict = NULL;
return true;
}
transformations will be permanent.
Returns true if anything changed, false otherwise. */
bool
-proc_cancel_temporary_transformations (void)
+proc_cancel_temporary_transformations (struct dataset *ds)
{
- if (proc_in_temporary_transformations ())
+ if (proc_in_temporary_transformations (ds))
{
- dict_destroy (default_dict);
- default_dict = permanent_dict;
- permanent_dict = NULL;
+ dict_destroy (ds->dict);
+ ds->dict = ds->permanent_dict;
+ ds->permanent_dict = NULL;
+ if (ds->replace_dict) ds->replace_dict (ds->dict);
- trns_chain_destroy (temporary_trns_chain);
- temporary_trns_chain = NULL;
+ trns_chain_destroy (ds->temporary_trns_chain);
+ ds->temporary_trns_chain = NULL;
return true;
}
/* Cancels all transformations, if any.
Returns true if successful, false on I/O error. */
bool
-proc_cancel_all_transformations (void)
+proc_cancel_all_transformations (struct dataset *ds)
{
bool ok;
- ok = trns_chain_destroy (permanent_trns_chain);
- ok = trns_chain_destroy (temporary_trns_chain) && ok;
- permanent_trns_chain = cur_trns_chain = trns_chain_create ();
- temporary_trns_chain = NULL;
+ assert (ds->proc_state == PROC_COMMITTED);
+ ok = trns_chain_destroy (ds->permanent_trns_chain);
+ ok = trns_chain_destroy (ds->temporary_trns_chain) && ok;
+ ds->permanent_trns_chain = ds->cur_trns_chain = trns_chain_create ();
+ ds->temporary_trns_chain = NULL;
return ok;
}
\f
/* Initializes procedure handling. */
-void
-proc_init (void)
+struct dataset *
+create_dataset (replace_source_callback *rps,
+ replace_dictionary_callback *rds)
{
- default_dict = dict_create ();
- proc_cancel_all_transformations ();
+ struct dataset *ds = xzalloc (sizeof(*ds));
+ ds->dict = dict_create ();
+ ds->caseinit = caseinit_create ();
+ ds->replace_source = rps;
+ ds->replace_dict = rds;
+ proc_cancel_all_transformations (ds);
+ return ds;
}
/* Finishes up procedure handling. */
void
-proc_done (void)
+destroy_dataset (struct dataset *ds)
{
- discard_variables ();
+ proc_discard_active_file (ds);
+ dict_destroy (ds->dict);
+ caseinit_destroy (ds->caseinit);
+ trns_chain_destroy (ds->permanent_trns_chain);
+ free (ds);
}
-/* Sets SINK as the destination for procedure output from the
- next procedure. */
+/* Causes output from the next procedure to be discarded, instead
+ of being preserved for use as input for the next procedure. */
void
-proc_set_sink (struct case_sink *sink)
+proc_discard_output (struct dataset *ds)
{
- assert (vfm_sink == NULL);
- vfm_sink = sink;
+ ds->discard_output = true;
+}
+
+/* Discards the active file dictionary, data, and
+ transformations. */
+void
+proc_discard_active_file (struct dataset *ds)
+{
+ assert (ds->proc_state == PROC_COMMITTED);
+
+ dict_clear (ds->dict);
+ fh_set_default_handle (NULL);
+
+ ds->n_lag = 0;
+
+ casereader_destroy (ds->source);
+ ds->source = NULL;
+ if ( ds->replace_source) ds->replace_source (NULL);
+
+ proc_cancel_all_transformations (ds);
}
/* Sets SOURCE as the source for procedure input for the next
procedure. */
void
-proc_set_source (struct case_source *source)
+proc_set_active_file (struct dataset *ds,
+ struct casereader *source,
+ struct dictionary *dict)
{
- assert (vfm_source == NULL);
- vfm_source = source;
+ assert (ds->proc_state == PROC_COMMITTED);
+ assert (ds->dict != dict);
+
+ proc_discard_active_file (ds);
+
+ dict_destroy (ds->dict);
+ ds->dict = dict;
+ if ( ds->replace_dict) ds->replace_dict (dict);
+
+ proc_set_active_file_data (ds, source);
}
-/* Returns true if a source for the next procedure has been
- configured, false otherwise. */
+/* Replaces the active file's data by READER without replacing
+ the associated dictionary. */
bool
-proc_has_source (void)
+proc_set_active_file_data (struct dataset *ds, struct casereader *reader)
{
- return vfm_source != NULL;
-}
+ casereader_destroy (ds->source);
+ ds->source = reader;
+ if (ds->replace_source) ds->replace_source (reader);
-/* Returns the output from the previous procedure.
- For use only immediately after executing a procedure.
- The returned casefile is owned by the caller; it will not be
- automatically used for the next procedure's input. */
-struct casefile *
-proc_capture_output (void)
-{
- struct casefile *casefile;
+ caseinit_clear (ds->caseinit);
+ caseinit_mark_as_preinited (ds->caseinit, ds->dict);
- /* Try to make sure that this function is called immediately
- after procedure() or a similar function. */
- assert (vfm_source != NULL);
- assert (case_source_is_class (vfm_source, &storage_source_class));
- assert (trns_chain_is_empty (permanent_trns_chain));
- assert (!proc_in_temporary_transformations ());
+ return reader == NULL || !casereader_error (reader);
+}
- casefile = storage_source_decapsulate (vfm_source);
- vfm_source = NULL;
+/* Returns true if an active file data source is available, false
+ otherwise. */
+bool
+proc_has_active_file (const struct dataset *ds)
+{
+ return ds->source != NULL;
+}
- return casefile;
+/* Checks whether DS has a corrupted active file. If so,
+ discards it and returns false. If not, returns true without
+ doing anything. */
+bool
+dataset_end_of_command (struct dataset *ds)
+{
+ if (ds->source != NULL)
+ {
+ if (casereader_error (ds->source))
+ {
+ proc_discard_active_file (ds);
+ return false;
+ }
+ else
+ {
+ const struct taint *taint = casereader_get_taint (ds->source);
+ taint_reset_successor_taint ((struct taint *) taint);
+ assert (!taint_has_tainted_successor (taint));
+ }
+ }
+ return true;
}
\f
static trns_proc_func case_limit_trns_proc;
static trns_free_func case_limit_trns_free;
/* Adds a transformation that limits the number of cases that may
- pass through, if default_dict has a case limit. */
+ pass through, if DS->DICT has a case limit. */
static void
-add_case_limit_trns (void)
+add_case_limit_trns (struct dataset *ds)
{
- size_t case_limit = dict_get_case_limit (default_dict);
+ size_t case_limit = dict_get_case_limit (ds->dict);
if (case_limit != 0)
{
size_t *cases_remaining = xmalloc (sizeof *cases_remaining);
*cases_remaining = case_limit;
- add_transformation (case_limit_trns_proc, case_limit_trns_free,
+ add_transformation (ds, case_limit_trns_proc, case_limit_trns_free,
cases_remaining);
- dict_set_case_limit (default_dict, 0);
+ dict_set_case_limit (ds->dict, 0);
}
}
*CASES_REMAINING. */
static int
case_limit_trns_proc (void *cases_remaining_,
- struct ccase *c UNUSED, int case_nr UNUSED)
+ struct ccase *c UNUSED, casenumber case_nr UNUSED)
{
size_t *cases_remaining = cases_remaining_;
- if (*cases_remaining > 0)
+ if (*cases_remaining > 0)
{
- *cases_remaining--;
+ (*cases_remaining)--;
return TRNS_CONTINUE;
}
else
/* Frees the data associated with a case limit transformation. */
static bool
-case_limit_trns_free (void *cases_remaining_)
+case_limit_trns_free (void *cases_remaining_)
{
size_t *cases_remaining = cases_remaining_;
free (cases_remaining);
/* Adds a temporary transformation to filter data according to
the variable specified on FILTER, if any. */
static void
-add_filter_trns (void)
+add_filter_trns (struct dataset *ds)
{
- struct variable *filter_var = dict_get_filter (default_dict);
- if (filter_var != NULL)
+ struct variable *filter_var = dict_get_filter (ds->dict);
+ if (filter_var != NULL)
{
- proc_start_temporary_transformations ();
- add_transformation (filter_trns_proc, NULL, filter_var);
+ proc_start_temporary_transformations (ds);
+ add_transformation (ds, filter_trns_proc, NULL, filter_var);
}
}
/* FILTER transformation. */
static int
filter_trns_proc (void *filter_var_,
- struct ccase *c UNUSED, int case_nr UNUSED)
-
+ struct ccase *c UNUSED, casenumber case_nr UNUSED)
+
{
struct variable *filter_var = filter_var_;
- double f = case_num (c, filter_var->fv);
- return (f != 0.0 && !mv_is_num_missing (&filter_var->miss, f)
+ double f = case_num (c, filter_var);
+ return (f != 0.0 && !var_is_num_missing (filter_var, f, MV_ANY)
? TRNS_CONTINUE : TRNS_DROP_CASE);
}
-\f
-static trns_proc_func process_if_trns_proc;
-static trns_free_func process_if_trns_free;
-/* Adds a temporary transformation to filter data according to
- the expression specified on PROCESS IF, if any. */
-static void
-add_process_if_trns (void)
+
+struct dictionary *
+dataset_dict (const struct dataset *ds)
{
- if (process_if_expr != NULL)
- {
- proc_start_temporary_transformations ();
- add_transformation (process_if_trns_proc, process_if_trns_free,
- process_if_expr);
- process_if_expr = NULL;
- }
+ return ds->dict;
}
-/* PROCESS IF transformation. */
-static int
-process_if_trns_proc (void *expression_,
- struct ccase *c UNUSED, int case_nr UNUSED)
-
+const struct casereader *
+dataset_source (const struct dataset *ds)
{
- struct expression *expression = expression_;
- return (expr_evaluate_num (expression, c, case_nr) == 1.0
- ? TRNS_CONTINUE : TRNS_DROP_CASE);
+ return ds->source;
}
-/* Frees a PROCESS IF transformation. */
-static bool
-process_if_trns_free (void *expression_)
+void
+dataset_need_lag (struct dataset *ds, int n_before)
{
- struct expression *expression = expression_;
- expr_free (expression);
- return true;
+ ds->n_lag = MAX (ds->n_lag, n_before);
}