2 * blncache.c: DAV baseline information cache.
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
24 #include <apr_pools.h>
27 #include "svn_dirent_uri.h"
28 #include "svn_types.h"
29 #include "svn_pools.h"
33 /* Baseline information cache object. */
34 typedef struct baseline_info_t
36 const char *bc_url; /* baseline collection URL. */
37 svn_revnum_t revision; /* revision associated with the baseline. */
41 /* Module-private structure used to hold the caches. */
42 struct svn_ra_serf__blncache_t
44 /* A hash mapping 'svn_revnum_t *' baseline revisions to 'const
45 * char *' baseline collection URLs.
47 apr_hash_t *revnum_to_bc;
49 /* A hash mapping 'const char *' baseline URLs to 'baseline_info_t *'
50 * structures. (Allocated from the same pool as 'revnum_to_bc'.)
52 apr_hash_t *baseline_info;
57 /* Return a pointer to an 'baseline_info_t' structure allocated from
58 * POOL and populated with BC_URL (which is duped into POOL) and
61 static baseline_info_t *
62 baseline_info_make(const char *bc_url,
63 svn_revnum_t revision,
66 baseline_info_t *result = apr_palloc(pool, sizeof(*result));
68 result->bc_url = apr_pstrdup(pool, bc_url);
69 result->revision = revision;
74 /* Set in HASH the value VAL for the KEY (whose key length is KLEN).
75 * KEY will be duped into HASH's pool.
78 hash_set_copy(apr_hash_t *hash,
83 if (klen == APR_HASH_KEY_STRING)
85 apr_hash_set(hash, apr_pmemdup(apr_hash_pool_get(hash), key, klen),
91 svn_ra_serf__blncache_create(svn_ra_serf__blncache_t **blncache_p,
94 svn_ra_serf__blncache_t *blncache = apr_pcalloc(pool, sizeof(*blncache));
95 apr_pool_t *cache_pool;
97 /* Create subpool for cached data. It will be cleared if we reach maximum
99 cache_pool = svn_pool_create(pool);
100 blncache->revnum_to_bc = apr_hash_make(cache_pool);
101 blncache->baseline_info = apr_hash_make(cache_pool);
103 *blncache_p = blncache;
108 #define MAX_CACHE_SIZE 1000
111 svn_ra_serf__blncache_set(svn_ra_serf__blncache_t *blncache,
112 const char *baseline_url,
113 svn_revnum_t revision,
115 apr_pool_t *scratch_pool)
117 if (bc_url && SVN_IS_VALID_REVNUM(revision))
119 apr_pool_t *cache_pool = apr_hash_pool_get(blncache->revnum_to_bc);
121 /* If the caches are too big, delete and recreate 'em and move along. */
122 if (MAX_CACHE_SIZE < (apr_hash_count(blncache->baseline_info)
123 + apr_hash_count(blncache->revnum_to_bc)))
125 svn_pool_clear(cache_pool);
126 blncache->revnum_to_bc = apr_hash_make(cache_pool);
127 blncache->baseline_info = apr_hash_make(cache_pool);
130 hash_set_copy(blncache->revnum_to_bc, &revision, sizeof(revision),
131 apr_pstrdup(cache_pool, bc_url));
135 hash_set_copy(blncache->baseline_info, baseline_url,
137 baseline_info_make(bc_url, revision, cache_pool));
144 #undef MAX_CACHE_SIZE
147 svn_ra_serf__blncache_get_bc_url(const char **bc_url_p,
148 svn_ra_serf__blncache_t *blncache,
150 apr_pool_t *result_pool)
152 const char *value = apr_hash_get(blncache->revnum_to_bc,
153 &revnum, sizeof(revnum));
154 *bc_url_p = value ? apr_pstrdup(result_pool, value) : NULL;
159 svn_ra_serf__blncache_get_baseline_info(const char **bc_url_p,
160 svn_revnum_t *revision_p,
161 svn_ra_serf__blncache_t *blncache,
162 const char *baseline_url,
165 baseline_info_t *info = svn_hash_gets(blncache->baseline_info, baseline_url);
168 *bc_url_p = apr_pstrdup(pool, info->bc_url);
169 *revision_p = info->revision;
174 *revision_p = SVN_INVALID_REVNUM;