FOSSology  4.4.0
Open Source License Compliance by Open Source Software
liccache.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2010-2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
15 #include "buckets.h"
16 
26 FUNCTION long lrcache_hash(cacheroot_t *pcroot, char *rf_shortname)
27 {
28  long hashval = 0;
29  int len, i;
30 
31  /* use the first sizeof(long) bytes for the hash value */
32  len = (strlen(rf_shortname) < sizeof(long)) ? strlen(rf_shortname) : sizeof(long);
33  for (i=0; i<len;i++) hashval += rf_shortname[i] << 8*i;
34  hashval = hashval % pcroot->maxnodes;
35  return hashval;
36 }
37 
46 FUNCTION void lrcache_print(cacheroot_t *pcroot)
47 {
48  cachenode_t *pcnode;
49  long hashval = 0;
50  int i;
51 
52  pcnode = pcroot->nodes;
53  for (i=0; i<pcroot->maxnodes; i++)
54  {
55  if (pcnode->rf_pk != 0L)
56  {
57  hashval = lrcache_hash(pcroot, pcnode->rf_shortname);
58  printf("%ld, %ld, %s\n", hashval, pcnode->rf_pk, pcnode->rf_shortname);
59  }
60  pcnode++;
61  }
62 }
63 
72 FUNCTION void lrcache_free(cacheroot_t *pcroot)
73 {
74  cachenode_t *pcnode;
75  int i;
76 
77  pcnode = pcroot->nodes;
78  for (i=0; i<pcroot->maxnodes; i++)
79  {
80  if (pcnode->rf_pk != 0L)
81  {
82  free(pcnode->rf_shortname);
83  }
84  pcnode++;
85  }
86  free(pcroot->nodes);
87 }
88 
99 FUNCTION int lrcache_add(cacheroot_t *pcroot, long rf_pk, char *rf_shortname)
100 {
101  cachenode_t *pcnode;
102  long hashval = 0;
103  int i;
104  int noden;
105 
106  hashval = lrcache_hash(pcroot, rf_shortname);
107 
108  noden = hashval;
109  for (i=0; i<pcroot->maxnodes; i++)
110  {
111  noden = (hashval +i) & (pcroot->maxnodes -1);
112 
113  pcnode = pcroot->nodes + noden;
114  if (!pcnode->rf_pk)
115  {
116  pcnode->rf_shortname = strdup(rf_shortname);
117  pcnode->rf_pk = rf_pk;
118  break;
119  }
120  }
121  if (i < pcroot->maxnodes) return 0;
122 
123  return -1; /* no space */
124 }
125 
135 FUNCTION long lrcache_lookup(cacheroot_t *pcroot, char *rf_shortname)
136 {
137  cachenode_t *pcnode;
138  long hashval = 0;
139  int i;
140  int noden;
141 
142  hashval = lrcache_hash(pcroot, rf_shortname);
143 
144  noden = hashval;
145  for (i=0; i<pcroot->maxnodes; i++)
146  {
147  noden = (hashval +i) & (pcroot->maxnodes -1);
148 
149  pcnode = pcroot->nodes + noden;
150  if (!pcnode->rf_pk) return 0;
151  if (strcmp(pcnode->rf_shortname, rf_shortname) == 0)
152  {
153  return pcnode->rf_pk;
154  }
155  }
156 
157  return 0; /* not found */
158 }
159 
174 FUNCTION int lrcache_init(PGconn *pgConn, cacheroot_t *pcroot)
175 {
176  PGresult *result;
177  char query[128];
178  int row;
179  int numLics;
180 
181  if (!pcroot) return 0;
182 
183  snprintf(query, sizeof(query),
184  "SELECT rf_pk, rf_shortname FROM license_ref where rf_detector_type=2;");
185  result = PQexec(pgConn, query);
186  if (fo_checkPQresult(pgConn, result, query, "lrcache_init", __LINE__)) return 0;
187 
188  numLics = PQntuples(result);
189  /* populate the cache */
190  for (row = 0; row < numLics; row++)
191  {
192  lrcache_add(pcroot, atol(PQgetvalue(result, row, 0)), PQgetvalue(result, row, 1));
193  }
194 
195  PQclear(result);
196 
197  return (1);
198 } /* lrcache_init */
199 
213 FUNCTION long get_rfpk(PGconn *pgConn, cacheroot_t *pcroot, char *rf_shortname)
214 {
215  long rf_pk;
216  size_t len;
217 
218  if ((len = strlen(rf_shortname)) == 0)
219  {
220  printf("ERROR! %s.%d get_rfpk() passed empty name", __FILE__, __LINE__);
221  return (0);
222  }
223 
224  /* is this in the cache? */
225  rf_pk = lrcache_lookup(pcroot, rf_shortname);
226  if (rf_pk) return rf_pk;
227 
228  /* shortname was not found, so add it */
229  /* add to the license_ref table */
230  rf_pk = add2license_ref(pgConn, rf_shortname);
231 
232  /* add to the cache */
233  lrcache_add(pcroot, rf_pk, rf_shortname);
234 
235  return (rf_pk);
236 } /* get_rfpk */
237 
246 FUNCTION long add2license_ref(PGconn *pgConn, char *licenseName)
247 {
248  PGresult *result;
249  char query[MAXSQL];
250  char insert[MAXSQL];
251  char escLicName[256];
252  char *specialLicenseText;
253  long rf_pk;
254 
255  int len;
256  int error;
257  int numRows;
258 
259  // escape the name
260  len = strlen(licenseName);
261  PQescapeStringConn(pgConn, escLicName, licenseName, len, &error);
262  if (error)
263  printf("WARNING: %s(%d): Does license name have multibyte encoding?", __FILE__, __LINE__);
264 
265  /* verify the license is not already in the table */
266  sprintf(query, "SELECT rf_pk FROM license_ref where rf_shortname='%s' and rf_detector_type=2", escLicName);
267  result = PQexec(pgConn, query);
268  if (fo_checkPQresult(pgConn, result, query, "add2license_ref", __LINE__)) return 0;
269  numRows = PQntuples(result);
270  if (numRows)
271  {
272  rf_pk = atol(PQgetvalue(result, 0, 0));
273  return rf_pk;
274  }
275 
276  /* Insert the new license */
277  specialLicenseText = "License by Nomos.";
278 
279  sprintf( insert,
280  "insert into license_ref(rf_shortname, rf_text, rf_detector_type) values('%s', '%s', 2)",
281  escLicName, specialLicenseText);
282  result = PQexec(pgConn, insert);
283  if (fo_checkPQcommand(pgConn, result, insert, __FILE__, __LINE__)) return 0;
284  PQclear(result);
285 
286  /* retrieve the new rf_pk */
287  result = PQexec(pgConn, query);
288  if (fo_checkPQresult(pgConn, result, query, "add2license_ref", __LINE__)) return 0;
289  numRows = PQntuples(result);
290  if (numRows)
291  rf_pk = atol(PQgetvalue(result, 0, 0));
292  else
293  {
294  printf("ERROR: %s:%s:%d Just inserted value is missing. On: %s", __FILE__, "add2license_ref()", __LINE__, query);
295  return(0);
296  }
297  PQclear(result);
298 
299  return (rf_pk);
300 }
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:170
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:204
FUNCTION void lrcache_print(cacheroot_t *pcroot)
Print the contents of the hash table.
Definition: liccache.c:46
FUNCTION void lrcache_free(cacheroot_t *pcroot)
Free the hash table.
Definition: liccache.c:72
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:135
FUNCTION long add2license_ref(PGconn *pgConn, char *licenseName)
Definition: liccache.c:246
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:99
FUNCTION long get_rfpk(PGconn *pgConn, cacheroot_t *pcroot, char *rf_shortname)
Get the rf_pk for rf_shortname.
Definition: liccache.c:213
FUNCTION int lrcache_init(PGconn *pgConn, cacheroot_t *pcroot)
Build a cache the license ref db table.
Definition: liccache.c:174
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:26
long rf_pk
License id from database.
Definition: liccache.h:32
char * rf_shortname
License shortname.
Definition: liccache.h:31
int maxnodes
No. of nodes in the list.
Definition: liccache.h:42
cachenode_t * nodes
Array of nodes.
Definition: liccache.h:43