summaryrefslogtreecommitdiff
path: root/src/commit/commit-algorithm.hpp
blob: 0c2980f72e90c3a8d3ee995f5dfa2fc2abcd2cfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2011 Vyatta, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#ifndef _COMMIT_ALGORITHM_HPP_
#define _COMMIT_ALGORITHM_HPP_
#include <vector>
#include <string>
#include <queue>
#include <tr1/memory>

#include <cnode/cnode-util.hpp>
#include <cstore/cpath.hpp>
#include <cstore/ctemplate.hpp>

// forward decl
namespace cnode {
class CfgNode;
}
namespace cstore {
class Cstore;
}

namespace commit {

using namespace cnode;
using namespace cstore;

enum CommitState {
  COMMIT_STATE_UNCHANGED,
  COMMIT_STATE_ADDED,
  COMMIT_STATE_DELETED,
  COMMIT_STATE_CHANGED
};

enum CommitTreeTraversalOrder {
  PRE_ORDER,
  POST_ORDER
};

enum CommitHook {
  PRE_COMMIT,
  POST_COMMIT,
  LAST // not a valid hook
};

class CommitData {
public:
  CommitData();
  virtual ~CommitData() {}

  // setters
  void setCommitState(CommitState s);
  void setCommitPath(const Cpath& p, bool is_val, const std::string& val,
                     const std::string& name);
  void setCommitMultiValues(const std::vector<std::string>& values,
                            const std::vector<CommitState>& states);
  void setCommitValue(const std::string& val1, const std::string& val2,
                      bool def1, bool def2);
  void setCommitChildDeleteFailed();
  void setCommitCreateFailed();
  void setCommitSubtreeChanged();

  // getters
  CommitState getCommitState() const;
  Cpath getCommitPath() const;
  size_t numCommitMultiValues() const;
  std::string commitMultiValueAt(size_t idx) const;
  CommitState commitMultiStateAt(size_t idx) const;
  std::string commitValueBefore() const;
  std::string commitValueAfter() const;
  bool commitChildDeleteFailed() const;
  bool commitCreateFailed() const;
  bool commitSubtreeChanged() const;

  // for tmpl stuff
  void setTmpl(std::tr1::shared_ptr<cstore::Ctemplate> def);
  std::tr1::shared_ptr<cstore::Ctemplate> getTmpl() const;
  const vtw_def *getDef() const;
  unsigned int getPriority() const;
  void setPriority(unsigned int p);
  const vtw_node *getActions(vtw_act_type act, bool raw = false) const;
  bool isBeginEndNode() const;

private:
  std::tr1::shared_ptr<cstore::Ctemplate> _def;
  Cpath _commit_path;
  CommitState _commit_state;
  std::vector<std::string> _commit_values;
  std::vector<CommitState> _commit_values_states;
  std::pair<std::string, std::string> _commit_value;
  std::pair<bool, bool> _commit_default;
  bool _commit_create_failed;
  bool _commit_child_delete_failed;
  bool _commit_subtree_changed;
};

class PrioNode : public TreeNode<PrioNode> {
public:
  PrioNode(CfgNode *n);
  ~PrioNode() {}

  CfgNode *getCfgNode();
  unsigned int getPriority() const;
  CommitState getCommitState() const;
  Cpath getCommitPath() const;
  bool parentCreateFailed() const;
  bool succeeded() const;
  bool hasSubtreeFailure() const;
  bool hasSubtreeSuccess() const;

  void setCfgParent(CfgNode *p);
  void setSucceeded(bool succeeded);
  void setSubtreeFailure();
  void setSubtreeSuccess();

private:
  CfgNode *_node;
  CfgNode *_cfg_parent;
  bool _succeeded;
  bool _subtree_failure;
  bool _subtree_success;
};

template<bool for_delete> struct PrioNodeCmp {
  inline bool operator()(PrioNode *a, PrioNode *b) {
    return _is_after(a, b, Int2Type<for_delete>());
  }

  /* note: if comparing "for delete", use "<". if not for delete, use ">".
   *       if two nodes have the same priority, the ordering between them
   *       is not defined, i.e., can be either.
   */
  inline bool _is_after(PrioNode *a, PrioNode *b, Int2Type<false>) {
    return (a->getPriority() > b->getPriority());
  }
  inline bool _is_after(PrioNode *a, PrioNode *b, Int2Type<true>) {
    return (a->getPriority() < b->getPriority());
  }
};

typedef std::priority_queue<PrioNode *, std::vector<PrioNode *>,
                            PrioNodeCmp<false> > PrioQueueT;
typedef std::priority_queue<PrioNode *, std::vector<PrioNode *>,
                            PrioNodeCmp<true> > DelPrioQueueT;

typedef std::pair<CommitState, std::tr1::shared_ptr<Cpath> >
  CommittedPathT;
typedef std::vector<CommittedPathT> CommittedPathListT;

// exported functions
const char *getCommitHookDir(CommitHook hook);
CfgNode *getCommitTree(CfgNode *cfg1, CfgNode *cfg2, const Cpath& cur_path);
bool isCommitPathEffective(Cstore& cs, const Cpath& pcomps,
                           std::tr1::shared_ptr<Ctemplate> def,
                           bool in_active, bool in_working);
bool doCommit(Cstore& cs, CfgNode& cfg1, CfgNode& cfg2);

} // namespace commit

#endif /* _COMMIT_ALGORITHM_HPP_ */