summaryrefslogtreecommitdiff
path: root/redland/redland/redland-1.0.8.patch.storage_hashes_list_duplicates
blob: d2ef7e772230e6ddb5ccab1600c37cdf0461066e (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
--- misc/redland-1.0.8/librdf/rdf_storage_hashes.c	Tue Jul  1 05:10:26 2008
+++ misc/build/redland-1.0.8/librdf/rdf_storage_hashes.c	Thu Nov  6 12:44:39 2008
@@ -1387,6 +1387,66 @@
                                                     LIBRDF_STATEMENT_OBJECT);
 }
 
+
+/* return -1 on failure, 1 if context contains stmt, 0 if not */
+static int
+librdf_storage_hashes_context_contains_statement(librdf_storage* storage,
+    librdf_node* context_node,
+    librdf_statement* statement)
+{
+  librdf_storage_hashes_context* context=(librdf_storage_hashes_context*)storage->context;
+  librdf_hash_datum key, value; /* on stack - not allocated */
+  size_t size;
+  int status;
+
+  if(context->contexts_index < 0) {
+    librdf_log(storage->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_STORAGE, NULL,
+               "Storage was created without context support");
+    return -1;
+  }
+
+  /* ENCODE KEY */
+  size=librdf_node_encode(context_node, NULL, 0);
+  if (!size)
+    return -1;
+  key.data=(char*)LIBRDF_MALLOC(cstring, size);
+  if (!key.data)
+    return -1;
+  key.size=librdf_node_encode(context_node,
+                               (unsigned char*)key.data, size);
+  if (!key.size) {
+    LIBRDF_FREE(data, key.data);
+    return -1;
+  }
+
+  /* ENCODE VALUE */
+  size=librdf_statement_encode(statement, NULL, 0);
+  if (!size) {
+    LIBRDF_FREE(data, key.data);
+    return -1;
+  }
+  value.data=(char*)LIBRDF_MALLOC(cstring, size);
+  if (!value.data) {
+    LIBRDF_FREE(data, key.data);
+    return -1;
+  }
+  value.size=librdf_statement_encode(statement, (unsigned char*)value.data, size);
+  if (!value.size) {
+    LIBRDF_FREE(data, value.data);
+    LIBRDF_FREE(data, key.data);
+    return -1;
+  }
+
+  status=librdf_hash_exists(context->hashes[context->contexts_index], &key, &value);
+  LIBRDF_FREE(data, value.data);
+  LIBRDF_FREE(data, key.data);
+
+  /* DO NOT free statement, ownership was not passed in */
+  return status;
+}
+
+
+
 /**
  * librdf_storage_hashes_context_add_statement:
  * @storage: #librdf_storage object
@@ -1412,7 +1472,15 @@
                "Storage was created without context support");
     return 1;
   }
-  
+
+  /* Do not add duplicate statements */
+  status=librdf_storage_hashes_context_contains_statement(storage, context_node, statement);
+  if(status)
+    if(status < 0)
+      return 1;
+    else
+      return 0;
+
   if(librdf_storage_hashes_add_remove_statement(storage, 
                                                 statement, context_node, 1))
     return 1;
--- misc/redland-1.0.8/librdf/rdf_storage_list.c	Tue Jul  1 05:10:26 2008
+++ misc/build/redland-1.0.8/librdf/rdf_storage_list.c	Thu Nov  6 12:44:39 2008
@@ -457,6 +457,64 @@
 }
 
 
+/* return -1 on failure, 1 if context contains stmt, 0 if not */
+static int
+librdf_storage_list_context_contains_statement(librdf_storage* storage,
+    librdf_node* context_node,
+    librdf_statement* statement)
+{
+  librdf_storage_list_context* context=(librdf_storage_list_context*)storage->context;
+  librdf_hash_datum key, value; /* on stack - not allocated */
+  size_t size;
+  int status;
+
+  if(!context->index_contexts) {
+    librdf_log(storage->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_STORAGE, NULL,
+               "Storage was created without context support");
+    return -1;
+  }
+
+  /* ENCODE KEY */
+  size=librdf_node_encode(context_node, NULL, 0);
+  if (!size)
+    return -1;
+  key.data=(char*)LIBRDF_MALLOC(cstring, size);
+  if (!key.data)
+    return -1;
+  key.size=librdf_node_encode(context_node,
+                               (unsigned char*)key.data, size);
+  if (!key.size) {
+    LIBRDF_FREE(data, key.data);
+    return -1;
+  }
+
+  /* ENCODE VALUE */
+  size=librdf_statement_encode(statement, NULL, 0);
+  if (!size) {
+    LIBRDF_FREE(data, key.data);
+    return -1;
+  }
+  value.data=(char*)LIBRDF_MALLOC(cstring, size);
+  if (!value.data) {
+    LIBRDF_FREE(data, key.data);
+    return -1;
+  }
+  value.size=librdf_statement_encode(statement, (unsigned char*)value.data, size);
+  if (!value.size) {
+    LIBRDF_FREE(data, value.data);
+    LIBRDF_FREE(data, key.data);
+    return -1;
+  }
+
+  status=librdf_hash_exists(context->contexts, &key, &value);
+  LIBRDF_FREE(data, value.data);
+  LIBRDF_FREE(data, key.data);
+
+  /* DO NOT free statement, ownership was not passed in */
+  return status;
+}
+
+
 /**
  * librdf_storage_list_context_add_statement:
  * @storage: #librdf_storage object
@@ -483,7 +541,15 @@
                "Storage was created without context support");
     return 1;
   }
-  
+
+  /* Do not add duplicate statements */
+  status=librdf_storage_list_context_contains_statement(storage, context_node, statement);
+  if(status)
+    if(status < 0)
+      return 1;
+    else
+      return 0;
+
   /* Store statement + node in the storage_list */
   sln=(librdf_storage_list_node*)LIBRDF_MALLOC(librdf_storage_list_node, sizeof(librdf_storage_list_node));
   if(!sln)