MongoDB C++ Driver  legacy-1.1.2
initializer_dependency_graph.h
1 /* Copyright 2012 10gen Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <string>
19 #include <vector>
20 #include <utility>
21 
22 #include "mongo/base/disallow_copying.h"
23 #include "mongo/base/initializer_function.h"
24 #include "mongo/base/status.h"
25 #include "mongo/platform/unordered_map.h"
26 #include "mongo/platform/unordered_set.h"
27 
28 namespace mongo {
29 
48  MONGO_DISALLOW_COPYING(InitializerDependencyGraph);
49 
50 public:
53 
65  Status addInitializer(const std::string& name,
66  const InitializerFunction& fn,
67  const std::vector<std::string>& prerequisites,
68  const std::vector<std::string>& dependents);
69 
74  InitializerFunction getInitializerFunction(const std::string& name) const;
75 
89  Status topSort(std::vector<std::string>* sortedNames) const;
90 
91 private:
92  struct NodeData {
94  unordered_set<std::string> prerequisites;
95  };
96 
97  typedef unordered_map<std::string, NodeData> NodeMap;
98  typedef NodeMap::value_type Node;
99 
103  static Status recursiveTopSort(const NodeMap& nodeMap,
104  const Node& currentNode,
105  std::vector<std::string>* inProgressNodeNames,
106  unordered_set<std::string>* visitedNodeNames,
107  std::vector<std::string>* sortedNames);
108 
114  NodeMap _nodes;
115 };
116 
117 } // namespace mongo
Status represents an error state or the absence thereof.
Definition: status.h:50
Representation of a dependency graph of "initialization operations.".
Definition: initializer_dependency_graph.h:47
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
Status topSort(std::vector< std::string > *sortedNames) const
Construct a topological sort of the dependency graph, and store that order into "sortedNames".
Status addInitializer(const std::string &name, const InitializerFunction &fn, const std::vector< std::string > &prerequisites, const std::vector< std::string > &dependents)
Add a new initializer node, named "name", to the dependency graph, with the given behavior...
stdx::function< Status(InitializerContext *)> InitializerFunction
An InitializerFunction implements the behavior of an initializer operation.
Definition: initializer_function.h:23
InitializerFunction getInitializerFunction(const std::string &name) const
Given a dependency operation node named "name", return its behavior function.