root/trunk/hash.c

Revision 7, 1.9 kB (checked in by jesus, 17 years ago)

All of George's <george@lethargy.org> vhost additions.
New time rewriting so that timestamps are rewritten to reflect the time on the local machine running spreadlogd -- me.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* ======================================================================
2  * Copyright (c) 2000 George Schlossnagle
3  * All rights reserved.
4  * The following code was written by George Schlossnagle <george@lethargy.org>
5  * This code was written to facilitate clustered logging via Spread,
6  * particularly in conjunction with the mod_log_spread module for Apache.
7  * More information on Spread can be found at http://www.spread.org/
8  * More information on mod_log_spread can be found at
9  * http://www.backhand.org/mod_log_spread/
10  * Please refer to the LICENSE file before using this software.
11  * ======================================================================
12 */
13
14 #include <stdlib.h>
15 #include <stdio.h>
16
17 #include "hash.h"
18
19 static int myprime[20] = {
20         3,5,7,11,13,17,23,31,37,41,43,47,53,59,61,67,71,73,83,87
21 };
22
23 int gethash(void *hostheader, hash_element *hash) {
24   int a, i;
25   hash_element *elem;
26   a = hashpjw(hostheader,NR_OPEN);
27   if(hash[a].fd == -1) return -1;  /* return -1 if element is not here */
28   for(i=0;i<NR_OPEN; i++) {
29     elem = &hash[(a+(i*myprime[a%20]))%NR_OPEN];
30     /* return -1 if element is not possibly in hsah*/
31     if (elem->fd == -1)
32       return -1;
33     /*return fd if the element matches */
34     if(!strcmp(hostheader,elem->hostheader))
35       return elem->fd;
36   }
37   return -1;
38 }
39
40 void inshash(hash_element b, hash_element *hash) {
41   int a, i;
42   a = hashpjw(b.hostheader,NR_OPEN);
43   for(i=0;i<NR_OPEN; i++)
44     if((hash[(a+(i*myprime[a%20]))%NR_OPEN].fd) == -1) {
45       hash[(a+(i*myprime[a%20]))%NR_OPEN] = b;
46       return;
47     }
48 }
49
50 int hashpjw(const void *key, const int size) {
51   const char *ptr;
52   unsigned int val = 0;
53   ptr = key;
54   while (*ptr != '\0') {
55     int tmp;
56     val = (val << 4) + (*ptr);
57     if ((tmp = (val & 0xf0000000)) != 0) {
58       val = val ^ (tmp >> 24);
59       val = val ^ tmp;
60     }
61     ptr++;
62   }
63   return (int) (val % size);
64 }
65
Note: See TracBrowser for help on using the browser.