Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / engine-controller / graph_edge_pool.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <assert.h>             /* TODO : replace with framework assert */
19 #include <glib.h>
20 #include <stdlib.h>
21 #include "utility/sync_util.h"
22 #include "engine-controller/graph_edge_pool.h"
23 #include "engine-controller/hash_set.h"
24
25 #ifndef SYNC_AGENT_LOG
26 #undef LOG_TAG
27 #define LOG_TAG "AF_EC"
28 #endif
29
30 struct ec_graph_edge_pool_s {
31         GHashTable *from_graph_node_hash;       /* containing ec_hash_set_t* to_graph_nodes */
32 };
33
34 ec_graph_edge_pool_t *ec_graph_edge_pool_new(void)
35 {
36         _EXTERN_FUNC_ENTER;
37
38         ec_graph_edge_pool_t *pool = (ec_graph_edge_pool_t *) calloc(1, sizeof(ec_graph_edge_pool_t));
39         if (pool == NULL) {
40                 goto error_part;
41         }
42
43         pool->from_graph_node_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
44         if (pool->from_graph_node_hash == NULL) {
45                 goto error_part;
46         }
47
48         _EXTERN_FUNC_EXIT;
49
50         return pool;
51
52  error_part:
53         ec_graph_edge_pool_free(pool);
54         return NULL;
55 }
56
57 void ec_graph_edge_pool_free(ec_graph_edge_pool_t * pool)
58 {
59         _EXTERN_FUNC_ENTER;
60
61         if (pool != NULL) {
62                 if (pool->from_graph_node_hash != NULL) {
63                         g_hash_table_destroy(pool->from_graph_node_hash);
64                 }
65                 free(pool);
66         }
67
68         _EXTERN_FUNC_EXIT;
69 }
70
71 /* return add success or not */
72 /* if already exist edge added, then return false */
73 sync_agent_ec_boolean ec_graph_edge_pool_add_edge(ec_graph_edge_pool_t * pool, sync_agent_ec_constpointer from_node, sync_agent_ec_constpointer to_node)
74 {
75         _EXTERN_FUNC_ENTER;
76
77         sync_agent_ec_boolean add_success = false;
78         ec_hash_set_t *to_node_set = NULL;
79
80         to_node_set = g_hash_table_lookup(pool->from_graph_node_hash, from_node);
81         if (to_node_set == NULL) {
82                 to_node_set = ec_hash_set_create();
83                 if (to_node_set == NULL) {
84                         assert(false);
85                 }
86                 g_hash_table_insert(pool->from_graph_node_hash, (gpointer) from_node, (gpointer) to_node_set);
87         }
88
89         add_success = ec_hash_set_add(to_node_set, to_node);
90
91         _EXTERN_FUNC_EXIT;
92
93         return add_success;
94 }
95
96 GList *ec_graph_edge_pool_query_to_node_list(ec_graph_edge_pool_t * pool, sync_agent_ec_constpointer from_node)
97 {
98         _EXTERN_FUNC_ENTER;
99
100         retvm_if(pool == NULL, NULL, "ec_graph_edge_pool_t is NULL !!");
101
102         GList *to_nodes_list = NULL;
103
104         ec_hash_set_t *to_node_set = NULL;
105         to_node_set = g_hash_table_lookup(pool->from_graph_node_hash, from_node);
106         if (to_node_set == NULL) {
107                 goto return_part;
108         }
109
110         to_nodes_list = ec_hash_set_get_element_list(to_node_set);
111
112  return_part:
113
114         _EXTERN_FUNC_EXIT;
115
116         return to_nodes_list;
117 }