FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
liccache.c
Go to the documentation of this file.
1 /***************************************************************
2  Copyright (C) 2010-2011 Hewlett-Packard Development Company, L.P.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17  ***************************************************************/
26 #include "buckets.h"
27 
37 FUNCTION long lrcache_hash(cacheroot_t *pcroot, char *rf_shortname)
38 {
39  long hashval = 0;
40  int len, i;
41 
42  /* use the first sizeof(long) bytes for the hash value */
43  len = (strlen(rf_shortname) < sizeof(long)) ? strlen(rf_shortname) : sizeof(long);
44  for (i=0; i<len;i++) hashval += rf_shortname[i] << 8*i;
45  hashval = hashval % pcroot->maxnodes;
46  return hashval;
47 }
48 
57 FUNCTION void lrcache_print(cacheroot_t *pcroot)
58 {
59  cachenode_t *pcnode;
60  long hashval = 0;
61  int i;
62 
63  pcnode = pcroot->nodes;
64  for (i=0; i<pcroot->maxnodes; i++)
65  {
66  if (pcnode->rf_pk != 0L)
67  {
68  hashval = lrcache_hash(pcroot, pcnode->rf_shortname);
69  printf("%ld, %ld, %s\n", hashval, pcnode->rf_pk, pcnode->rf_shortname);
70  }
71  pcnode++;
72  }
73 }
74 
83 FUNCTION void lrcache_free(cacheroot_t *pcroot)
84 {
85  cachenode_t *pcnode;
86  int i;
87 
88  pcnode = pcroot->nodes;
89  for (i=0; i<pcroot->maxnodes; i++)
90  {
91  if (pcnode->rf_pk != 0L)
92  {
93  free(pcnode->rf_shortname);
94  }
95  pcnode++;
96  }
97  free(pcroot->nodes);
98 }
99 
110 FUNCTION int lrcache_add(cacheroot_t *pcroot, long rf_pk, char *rf_shortname)
111 {
112  cachenode_t *pcnode;
113  long hashval = 0;
114  int i;
115  int noden;
116 
117  hashval = lrcache_hash(pcroot, rf_shortname);
118 
119  noden = hashval;
120  for (i=0; i<pcroot->maxnodes; i++)
121  {
122  noden = (hashval +i) & (pcroot->maxnodes -1);
123 
124  pcnode = pcroot->nodes + noden;
125  if (!pcnode->rf_pk)
126  {
127  pcnode->rf_shortname = strdup(rf_shortname);
128  pcnode->rf_pk = rf_pk;
129  break;
130  }
131  }
132  if (i < pcroot->maxnodes) return 0;
133 
134  return -1; /* no space */
135 }
136 
146 FUNCTION long lrcache_lookup(cacheroot_t *pcroot, char *rf_shortname)
147 {
148  cachenode_t *pcnode;
149  long hashval = 0;
150  int i;
151  int noden;
152 
153  hashval = lrcache_hash(pcroot, rf_shortname);
154 
155  noden = hashval;
156  for (i=0; i<pcroot->maxnodes; i++)
157  {
158  noden = (hashval +i) & (pcroot->maxnodes -1);
159 
160  pcnode = pcroot->nodes + noden;
161  if (!pcnode->rf_pk) return 0;
162  if (strcmp(pcnode->rf_shortname, rf_shortname) == 0)
163  {
164  return pcnode->rf_pk;
165  }
166  }
167 
168  return 0; /* not found */
169 }
170 
185 FUNCTION int lrcache_init(PGconn *pgConn, cacheroot_t *pcroot)
186 {
187  PGresult *result;
188  char query[128];
189  int row;
190  int numLics;
191 
192  if (!pcroot) return 0;
193 
194  snprintf(query, sizeof(query),
195  "SELECT rf_pk, rf_shortname FROM license_ref where rf_detector_type=2;");
196  result = PQexec(pgConn, query);
197  if (fo_checkPQresult(pgConn, result, query, "lrcache_init", __LINE__)) return 0;
198 
199  numLics = PQntuples(result);
200  /* populate the cache */
201  for (row = 0; row < numLics; row++)
202  {
203  lrcache_add(pcroot, atol(PQgetvalue(result, row, 0)), PQgetvalue(result, row, 1));
204  }
205 
206  PQclear(result);
207 
208  return (1);
209 } /* lrcache_init */
210 
224 FUNCTION long get_rfpk(PGconn *pgConn, cacheroot_t *pcroot, char *rf_shortname)
225 {
226  long rf_pk;
227  size_t len;
228 
229  if ((len = strlen(rf_shortname)) == 0)
230  {
231  printf("ERROR! %s.%d get_rfpk() passed empty name", __FILE__, __LINE__);
232  return (0);
233  }
234 
235  /* is this in the cache? */
236  rf_pk = lrcache_lookup(pcroot, rf_shortname);
237  if (rf_pk) return rf_pk;
238 
239  /* shortname was not found, so add it */
240  /* add to the license_ref table */
241  rf_pk = add2license_ref(pgConn, rf_shortname);
242 
243  /* add to the cache */
244  lrcache_add(pcroot, rf_pk, rf_shortname);
245 
246  return (rf_pk);
247 } /* get_rfpk */
248 
257 FUNCTION long add2license_ref(PGconn *pgConn, char *licenseName)
258 {
259  PGresult *result;
260  char query[MAXSQL];
261  char insert[MAXSQL];
262  char escLicName[256];
263  char *specialLicenseText;
264  long rf_pk;
265 
266  int len;
267  int error;
268  int numRows;
269 
270  // escape the name
271  len = strlen(licenseName);
272  PQescapeStringConn(pgConn, escLicName, licenseName, len, &error);
273  if (error)
274  printf("WARNING: %s(%d): Does license name have multibyte encoding?", __FILE__, __LINE__);
275 
276  /* verify the license is not already in the table */
277  sprintf(query, "SELECT rf_pk FROM license_ref where rf_shortname='%s' and rf_detector_type=2", escLicName);
278  result = PQexec(pgConn, query);
279  if (fo_checkPQresult(pgConn, result, query, "add2license_ref", __LINE__)) return 0;
280  numRows = PQntuples(result);
281  if (numRows)
282  {
283  rf_pk = atol(PQgetvalue(result, 0, 0));
284  return rf_pk;
285  }
286 
287  /* Insert the new license */
288  specialLicenseText = "License by Nomos.";
289 
290  sprintf( insert,
291  "insert into license_ref(rf_shortname, rf_text, rf_detector_type) values('%s', '%s', 2)",
292  escLicName, specialLicenseText);
293  result = PQexec(pgConn, insert);
294  if (fo_checkPQcommand(pgConn, result, insert, __FILE__, __LINE__)) return 0;
295  PQclear(result);
296 
297  /* retrieve the new rf_pk */
298  result = PQexec(pgConn, query);
299  if (fo_checkPQresult(pgConn, result, query, "add2license_ref", __LINE__)) return 0;
300  numRows = PQntuples(result);
301  if (numRows)
302  rf_pk = atol(PQgetvalue(result, 0, 0));
303  else
304  {
305  printf("ERROR: %s:%s:%d Just inserted value is missing. On: %s", __FILE__, "add2license_ref()", __LINE__, query);
306  return(0);
307  }
308  PQclear(result);
309 
310  return (rf_pk);
311 }
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:181
FUNCTION long add2license_ref(PGconn *pgConn, char *licenseName)
Definition: liccache.c:257
int maxnodes
No. of nodes in the list.
Definition: liccache.h:53
cachenode_t * nodes
Array of nodes.
Definition: liccache.h:54
FUNCTION long lrcache_lookup(cacheroot_t *pcroot, char *rf_shortname)
Lookup rf_pk in the license_ref cache rf_shortname is the key.
Definition: liccache.c:146
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
FUNCTION int lrcache_add(cacheroot_t *pcroot, long rf_pk, char *rf_shortname)
Add a rf_shortname, rf_pk to the license_ref cache rf_shortname is the key.
Definition: liccache.c:110
FUNCTION long lrcache_hash(cacheroot_t *pcroot, char *rf_shortname)
Calculate the hash of an rf_shortname rf_shortname is the key.
Definition: liccache.c:37
FUNCTION void lrcache_free(cacheroot_t *pcroot)
Free the hash table.
Definition: liccache.c:83
FUNCTION long get_rfpk(PGconn *pgConn, cacheroot_t *pcroot, char *rf_shortname)
Get the rf_pk for rf_shortname.
Definition: liccache.c:224
FUNCTION void lrcache_print(cacheroot_t *pcroot)
Print the contents of the hash table.
Definition: liccache.c:57
FUNCTION int lrcache_init(PGconn *pgConn, cacheroot_t *pcroot)
Build a cache the license ref db table.
Definition: liccache.c:185
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
Definition: libfossdb.c:215
char * rf_shortname
License shortname.
Definition: liccache.h:42
long rf_pk
License id from database.
Definition: liccache.h:43