]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/test_cache_digest.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / test_cache_digest.cc
index 30ae1dbf0989a6b7c1ba841f6cd7cd249a3060ed..4ecfa677a820640e276e14a15a5ab2fdc433deaf 100644 (file)
@@ -1,35 +1,9 @@
-
 /*
- * $Id$
- *
- * AUTHOR: Alex Rousskov
- *
- * SQUID Web Proxy Cache          http://www.squid-cache.org/
- * ----------------------------------------------------------
- *
- *  Squid is the result of efforts by numerous individuals from
- *  the Internet community; see the CONTRIBUTORS file for full
- *  details.   Many organizations have provided support for Squid's
- *  development; see the SPONSORS file for full details.  Squid is
- *  Copyrighted (C) 2001 by the Regents of the University of
- *  California; see the COPYRIGHT file for full details.  Squid
- *  incorporates software developed and/or copyrighted by other
- *  sources; see the CREDITS file for full details.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
  *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
 /*
  */
 
 #include "squid.h"
+#include "CacheDigest.h"
+#include "store_key_md5.h"
+
+#include <cerrno>
 
 typedef struct {
     int query_count;
@@ -54,13 +32,12 @@ struct _Cache {
     CacheDigest *digest;
     Cache *peer;
     CacheQueryStats qstats;
-    int count;                 /* #currently cached entries */
-    int req_count;             /* #requests to this cache */
-    int bad_add_count;         /* #duplicate adds */
-    int bad_del_count;         /* #dels with no prior add */
+    int count;          /* #currently cached entries */
+    int req_count;      /* #requests to this cache */
+    int bad_add_count;      /* #duplicate adds */
+    int bad_del_count;      /* #dels with no prior add */
 };
 
-
 typedef struct _CacheEntry {
     const cache_key *key;
 
@@ -74,7 +51,7 @@ typedef struct _CacheEntry {
 typedef struct {
     cache_key key[SQUID_MD5_DIGEST_LENGTH];
     time_t timestamp;
-    short int use_icp;         /* true/false */
+    short int use_icp;      /* true/false */
 } RawAccessLogEntry;
 
 typedef enum {
@@ -87,29 +64,17 @@ typedef fr_result(*FI_READER) (FileIterator * fi);
 struct _FileIterator {
     const char *fname;
     FILE *file;
-    time_t inner_time;         /* timestamp of the current entry */
-    time_t time_offset;                /* to adjust time set by reader */
-    int line_count;            /* number of lines scanned */
-    int bad_line_count;                /* number of parsing errors */
-    int time_warp_count;       /* number of out-of-order entries in the file */
-    FI_READER reader;          /* reads next entry and updates inner_time */
-    void *entry;               /* buffer for the current entry, freed with xfree() */
+    time_t inner_time;      /* timestamp of the current entry */
+    time_t time_offset;     /* to adjust time set by reader */
+    int line_count;     /* number of lines scanned */
+    int bad_line_count;     /* number of parsing errors */
+    int time_warp_count;    /* number of out-of-order entries in the file */
+    FI_READER reader;       /* reads next entry and updates inner_time */
+    void *entry;        /* buffer for the current entry, freed with xfree() */
 };
 
 /* globals */
-static time_t cur_time = -1;   /* timestamp of the current log entry */
-
-/* copied from url.c */
-const char *RequestMethodStr[] = {
-    "NONE",
-    "GET",
-    "POST",
-    "PUT",
-    "HEAD",
-    "CONNECT",
-    "TRACE",
-    "PURGE"
-};
+static time_t cur_time = -1;    /* timestamp of the current log entry */
 
 /* copied from url.c */
 static HttpRequestMethod
@@ -190,7 +155,7 @@ fileIteratorAdvance(FileIterator * fi)
         const time_t last_time = fi->inner_time;
         fi->inner_time = -1;
         res = fi->reader(fi);
-        fi->line_count++;
+        ++ fi->line_count;
 
         if (fi->inner_time < 0)
             fi->inner_time = last_time;
@@ -198,14 +163,14 @@ fileIteratorAdvance(FileIterator * fi)
             fi->inner_time += fi->time_offset;
 
         if (res == frError)
-            fi->bad_line_count++;
+            ++ fi->bad_line_count;
         else if (res == frEof) {
             fprintf(stderr, "exhausted %s (%d entries) at %s",
                     fi->fname, fi->line_count, ctime(&fi->inner_time));
             fi->inner_time = -1;
         } else if (fi->inner_time < last_time) {
             assert(last_time >= 0);
-            fi->time_warp_count++;
+            ++ fi->time_warp_count;
             fi->inner_time = last_time;
         }
 
@@ -237,7 +202,6 @@ cacheEntryDestroy(CacheEntry * e)
     xfree(e);
 }
 
-
 /* Cache */
 
 static Cache *
@@ -269,9 +233,7 @@ cacheDestroy(Cache * cache)
     /* destroy the hash table itself */
     hashFreeMemory(hash);
 
-    if (cache->digest)
-        cacheDigestDestroy(cache->digest);
-
+    delete cache->digest;
     xfree(cache);
 }
 
@@ -287,12 +249,10 @@ cacheResetDigest(Cache * cache)
     assert(cache);
     fprintf(stderr, "%s: init-ing digest with %d entries\n", cache->name, cache->count);
 
-    if (cache->digest)
-        cacheDigestDestroy(cache->digest);
-
     hash = cache->hash;
 
-    cache->digest = cacheDigestCreate(cache->count + 1, 6);
+    delete cache->digest;
+    cache->digest = new CacheDigest(cache->count + 1, 6);
 
     if (!cache->count)
         return;
@@ -302,7 +262,7 @@ cacheResetDigest(Cache * cache)
     hash_first(hash);
 
     while ((e = (CacheEntry *)hash_next(hash))) {
-        cacheDigestAdd(cache->digest, e->key);
+        cache->digest->add(e->key);
     }
 
     gettimeofday(&t_end, NULL);
@@ -330,20 +290,20 @@ static void
 cacheQueryPeer(Cache * cache, const cache_key * key)
 {
     const int peer_has_it = hash_lookup(cache->peer->hash, key) != NULL;
-    const int we_think_we_have_it = cacheDigestTest(cache->digest, key);
+    const int we_think_we_have_it = cache->digest->test(key);
 
-    cache->qstats.query_count++;
+    ++ cache->qstats.query_count;
 
     if (peer_has_it) {
         if (we_think_we_have_it)
-            cache->qstats.true_hit_count++;
+            ++ cache->qstats.true_hit_count;
         else
-            cache->qstats.false_miss_count++;
+            ++ cache->qstats.false_miss_count;
     } else {
         if (we_think_we_have_it)
-            cache->qstats.false_hit_count++;
+            ++ cache->qstats.false_hit_count;
         else
-            cache->qstats.true_miss_count++;
+            ++ cache->qstats.true_miss_count;
     }
 }
 
@@ -383,7 +343,7 @@ static void
 cacheFetch(Cache * cache, const RawAccessLogEntry * e)
 {
     assert(e);
-    cache->req_count++;
+    ++ cache->req_count;
 
     if (e->use_icp)
         cacheQueryPeer(cache, e->key);
@@ -432,7 +392,7 @@ accessLogReader(FileIterator * fi)
     entry = (RawAccessLogEntry*)fi->entry;
 
     if (!fgets(buf, sizeof(buf), fi->file))
-        return frEof;          /* eof */
+        return frEof;       /* eof */
 
     entry->timestamp = fi->inner_time = (time_t) atoi(buf);
 
@@ -465,9 +425,9 @@ accessLogReader(FileIterator * fi)
     }
 
     while (*url)
-        url--;
+        --url;
 
-    url++;
+    ++url;
 
     *hier = '\0';
 
@@ -498,24 +458,23 @@ accessLogReader(FileIterator * fi)
     return frOk;
 }
 
-
 static void
 cachePurge(Cache * cache, storeSwapLogData * s, int update_digest)
 {
     CacheEntry *olde = (CacheEntry *) hash_lookup(cache->hash, s->key);
 
     if (!olde) {
-        cache->bad_del_count++;
+        ++ cache->bad_del_count;
     } else {
         assert(cache->count);
         hash_remove_link(cache->hash, (hash_link *) olde);
 
         if (update_digest)
-            cacheDigestDel(cache->digest, s->key);
+            cache->digest->remove(s->key);
 
         cacheEntryDestroy(olde);
 
-        cache->count--;
+        -- cache->count;
     }
 }
 
@@ -525,14 +484,14 @@ cacheStore(Cache * cache, storeSwapLogData * s, int update_digest)
     CacheEntry *olde = (CacheEntry *) hash_lookup(cache->hash, s->key);
 
     if (olde) {
-        cache->bad_add_count++;
+        ++ cache->bad_add_count;
     } else {
         CacheEntry *e = cacheEntryCreate(s);
         hash_join(cache->hash, (hash_link *)&e->key);
-        cache->count++;
+        ++ cache->count;
 
         if (update_digest)
-            cacheDigestAdd(cache->digest, e->key);
+            cache->digest->add(e->key);
     }
 }
 
@@ -559,7 +518,7 @@ usage(const char *prg_name)
 {
     fprintf(stderr, "usage: %s <access_log> <swap_state> ...\n",
             prg_name);
-    return -1;
+    return EXIT_FAILURE;
 }
 
 int
@@ -622,7 +581,7 @@ main(int argc, char *argv[])
     /* digest peer cache content */
     cacheResetDigest(them);
 
-    us->digest = cacheDigestClone(them->digest);       /* @netw@ */
+    us->digest = them->digest->clone();
 
     /* shift the time in access log to match ready_time */
     fileIteratorSetCurTime(fis[0], ready_time);
@@ -642,7 +601,7 @@ main(int argc, char *argv[])
                     next_time = fis[i]->inner_time;
                 }
 
-                active_fi_count++;
+                ++active_fi_count;
             }
         }
 
@@ -674,5 +633,6 @@ main(int argc, char *argv[])
     xfree(fis);
     cacheDestroy(them);
     cacheDestroy(us);
-    return 0;
+    return EXIT_SUCCESS;
 }
+