| 1 | /* |
| 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | * |
| 6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
| 7 | */ |
| 8 | |
| 9 | #include "monetdb_config.h" |
| 10 | #include "sql_catalog.h" |
| 11 | |
| 12 | void |
| 13 | cs_new(changeset * cs, sql_allocator *sa, fdestroy destroy) |
| 14 | { |
| 15 | cs->sa = sa; |
| 16 | cs->destroy = destroy; |
| 17 | cs->set = NULL; |
| 18 | cs->dset = NULL; |
| 19 | cs->nelm = NULL; |
| 20 | } |
| 21 | |
| 22 | void |
| 23 | cs_destroy(changeset * cs) |
| 24 | { |
| 25 | if (cs->set) { |
| 26 | list_destroy(cs->set); |
| 27 | cs->set = NULL; |
| 28 | } |
| 29 | if (cs->dset) { |
| 30 | list_destroy(cs->dset); |
| 31 | cs->dset = NULL; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | cs_add(changeset * cs, void *elm, int flags) |
| 37 | { |
| 38 | if (!cs->set) |
| 39 | cs->set = list_new(cs->sa, cs->destroy); |
| 40 | list_append(cs->set, elm); |
| 41 | if (newFlagSet(flags) && !cs->nelm) |
| 42 | cs->nelm = cs->set->t; |
| 43 | } |
| 44 | |
| 45 | void * |
| 46 | cs_transverse_with_validate(changeset * cs, void *elm, fvalidate cmp) |
| 47 | { |
| 48 | return list_traverse_with_validate(cs->set, elm, cmp); |
| 49 | } |
| 50 | |
| 51 | void* |
| 52 | cs_add_with_validate(changeset * cs, void *elm, int flags, fvalidate cmp) |
| 53 | { |
| 54 | void* res = NULL; |
| 55 | if (!cs->set) |
| 56 | cs->set = list_new(cs->sa, cs->destroy); |
| 57 | if((res = list_append_with_validate(cs->set, elm, cmp)) != NULL) |
| 58 | return res; |
| 59 | if (newFlagSet(flags) && !cs->nelm) |
| 60 | cs->nelm = cs->set->t; |
| 61 | return res; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | cs_add_before(changeset * cs, node *n, void *elm) |
| 66 | { |
| 67 | list_append_before(cs->set, n, elm); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | cs_del(changeset * cs, node *elm, int flags) |
| 72 | { |
| 73 | if (newFlagSet(flags)) { /* remove just added */ |
| 74 | if (cs->nelm == elm) |
| 75 | cs->nelm = elm->next; |
| 76 | list_remove_node(cs->set, elm); |
| 77 | } else { |
| 78 | if (!cs->dset) |
| 79 | cs->dset = list_new(cs->sa, cs->destroy); |
| 80 | list_move_data(cs->set, cs->dset, elm->data); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | cs_move(changeset *from, changeset *to, void *data) |
| 86 | { |
| 87 | if (!to->set) |
| 88 | to->set = list_new(to->sa, to->destroy); |
| 89 | list_move_data(from->set, to->set, data); |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | cs_size(changeset * cs) |
| 94 | { |
| 95 | if (cs->set) |
| 96 | return list_length(cs->set); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | node * |
| 101 | cs_first_node(changeset * cs) |
| 102 | { |
| 103 | return cs->set->h; |
| 104 | } |
| 105 | |
| 106 | node * |
| 107 | cs_last_node(changeset * cs) |
| 108 | { |
| 109 | return cs->set->t; |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | cs_remove_node(changeset * cs, node *n) |
| 114 | { |
| 115 | node *nxt = n->next; |
| 116 | |
| 117 | list_remove_node(cs->set, n); |
| 118 | if (cs->nelm == n) |
| 119 | cs->nelm = nxt; |
| 120 | } |
| 121 | |