sortedcache.c 8.81 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9 10 11 12 13 14 15 16 17 18
#include "sortedcache.h"

int git_sortedcache_new(
	git_sortedcache **out,
	size_t item_path_offset,
	git_sortedcache_free_item_fn free_item,
	void *free_item_payload,
	git_vector_cmp item_cmp,
	const char *path)
{
	git_sortedcache *sc;
19
	size_t pathlen, alloclen;
20 21 22

	pathlen = path ? strlen(path) : 0;

23 24 25
	GITERR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_sortedcache), pathlen);
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
	sc = git__calloc(1, alloclen);
26 27
	GITERR_CHECK_ALLOC(sc);

28 29 30
	git_pool_init(&sc->pool, 1);

	if (git_vector_init(&sc->items, 4, item_cmp) < 0 ||
31
		git_strmap_alloc(&sc->map) < 0)
32 33
		goto fail;

34
	if (git_rwlock_init(&sc->lock)) {
35
		giterr_set(GITERR_OS, "failed to initialize lock");
36 37 38
		goto fail;
	}

39 40
	sc->item_path_offset  = item_path_offset;
	sc->free_item         = free_item;
41 42 43 44 45 46 47 48 49
	sc->free_item_payload = free_item_payload;
	GIT_REFCOUNT_INC(sc);
	if (pathlen)
		memcpy(sc->path, path, pathlen);

	*out = sc;
	return 0;

fail:
50
	git_strmap_free(sc->map);
51 52 53 54 55 56 57 58 59 60 61
	git_vector_free(&sc->items);
	git_pool_clear(&sc->pool);
	git__free(sc);
	return -1;
}

void git_sortedcache_incref(git_sortedcache *sc)
{
	GIT_REFCOUNT_INC(sc);
}

62 63 64 65 66
const char *git_sortedcache_path(git_sortedcache *sc)
{
	return sc->path;
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static void sortedcache_clear(git_sortedcache *sc)
{
	git_strmap_clear(sc->map);

	if (sc->free_item) {
		size_t i;
		void *item;

		git_vector_foreach(&sc->items, i, item) {
			sc->free_item(sc->free_item_payload, item);
		}
	}

	git_vector_clear(&sc->items);

	git_pool_clear(&sc->pool);
}

static void sortedcache_free(git_sortedcache *sc)
{
87 88
	/* acquire write lock to make sure everyone else is done */
	if (git_sortedcache_wlock(sc) < 0)
89 90 91 92 93 94
		return;

	sortedcache_clear(sc);
	git_vector_free(&sc->items);
	git_strmap_free(sc->map);

95
	git_sortedcache_wunlock(sc);
96

97
	git_rwlock_free(&sc->lock);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	git__free(sc);
}

void git_sortedcache_free(git_sortedcache *sc)
{
	if (!sc)
		return;
	GIT_REFCOUNT_DEC(sc, sortedcache_free);
}

static int sortedcache_copy_item(void *payload, void *tgt_item, void *src_item)
{
	git_sortedcache *sc = payload;
	/* path will already have been copied by upsert */
	memcpy(tgt_item, src_item, sc->item_path_offset);
	return 0;
}

/* copy a sorted cache */
int git_sortedcache_copy(
	git_sortedcache **out,
	git_sortedcache *src,
120
	bool lock,
121 122 123
	int (*copy_item)(void *payload, void *tgt_item, void *src_item),
	void *payload)
{
124
	int error = 0;
125 126 127 128
	git_sortedcache *tgt;
	size_t i;
	void *src_item, *tgt_item;

129
	/* just use memcpy if no special copy fn is passed in */
130 131 132 133 134
	if (!copy_item) {
		copy_item = sortedcache_copy_item;
		payload   = src;
	}

135
	if ((error = git_sortedcache_new(
136 137
			&tgt, src->item_path_offset,
			src->free_item, src->free_item_payload,
138 139
			src->items._cmp, src->path)) < 0)
		return error;
140

141
	if (lock && git_sortedcache_rlock(src) < 0) {
142 143 144 145 146
		git_sortedcache_free(tgt);
		return -1;
	}

	git_vector_foreach(&src->items, i, src_item) {
147 148 149 150 151
		char *path = ((char *)src_item) + src->item_path_offset;

		if ((error = git_sortedcache_upsert(&tgt_item, tgt, path)) < 0 ||
			(error = copy_item(payload, tgt_item, src_item)) < 0)
			break;
152 153
	}

154 155
	if (lock)
		git_sortedcache_runlock(src);
156 157
	if (error)
		git_sortedcache_free(tgt);
158

159
	*out = !error ? tgt : NULL;
160

161
	return error;
162 163
}

164 165
/* lock sortedcache while making modifications */
int git_sortedcache_wlock(git_sortedcache *sc)
166
{
167
	GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */
168

169
	if (git_rwlock_wrlock(&sc->lock) < 0) {
170
		giterr_set(GITERR_OS, "unable to acquire write lock on cache");
171 172 173
		return -1;
	}
	return 0;
174 175
}

176 177
/* unlock sorted cache when done with modifications */
void git_sortedcache_wunlock(git_sortedcache *sc)
178
{
179 180
	git_vector_sort(&sc->items);
	git_rwlock_wrunlock(&sc->lock);
181 182
}

183 184
/* lock sortedcache for read */
int git_sortedcache_rlock(git_sortedcache *sc)
185
{
186
	GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */
187

188
	if (git_rwlock_rdlock(&sc->lock) < 0) {
189
		giterr_set(GITERR_OS, "unable to acquire read lock on cache");
190 191 192 193 194
		return -1;
	}
	return 0;
}

195 196
/* unlock sorted cache when done reading */
void git_sortedcache_runlock(git_sortedcache *sc)
197
{
198 199
	GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */
	git_rwlock_rdunlock(&sc->lock);
200 201 202 203 204 205 206 207
}

/* if the file has changed, lock cache and load file contents into buf;
 * returns <0 on error, >0 if file has not changed
 */
int git_sortedcache_lockandload(git_sortedcache *sc, git_buf *buf)
{
	int error, fd;
208
	struct stat st;
209

210
	if ((error = git_sortedcache_wlock(sc)) < 0)
211 212 213 214 215
		return error;

	if ((error = git_futils_filestamp_check(&sc->stamp, sc->path)) <= 0)
		goto unlock;

216 217 218 219 220 221 222
	if ((fd = git_futils_open_ro(sc->path)) < 0) {
		error = fd;
		goto unlock;
	}

	if (p_fstat(fd, &st) < 0) {
		giterr_set(GITERR_OS, "failed to stat file");
223
		error = -1;
224
		(void)p_close(fd);
225 226 227
		goto unlock;
	}

228
	if (!git__is_sizet(st.st_size)) {
229
		giterr_set(GITERR_INVALID, "unable to load file larger than size_t");
230 231
		error = -1;
		(void)p_close(fd);
232 233 234 235
		goto unlock;
	}

	if (buf)
236
		error = git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size);
237 238 239 240 241 242 243 244 245

	(void)p_close(fd);

	if (error < 0)
		goto unlock;

	return 1; /* return 1 -> file needs reload and was successfully loaded */

unlock:
246
	git_sortedcache_wunlock(sc);
247 248 249
	return error;
}

250 251
void git_sortedcache_updated(git_sortedcache *sc)
{
252 253
	/* update filestamp to latest value */
	git_futils_filestamp_check(&sc->stamp, sc->path);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
}

/* release all items in sorted cache */
int git_sortedcache_clear(git_sortedcache *sc, bool wlock)
{
	if (wlock && git_sortedcache_wlock(sc) < 0)
		return -1;

	sortedcache_clear(sc);

	if (wlock)
		git_sortedcache_wunlock(sc);

	return 0;
}

270
/* find and/or insert item, returning pointer to item data */
271
int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key)
272 273 274 275
{
	int error = 0;
	khiter_t pos;
	void *item;
276
	size_t keylen, itemlen;
277 278 279 280 281 282 283 284
	char *item_key;

	pos = git_strmap_lookup_index(sc->map, key);
	if (git_strmap_valid_index(sc->map, pos)) {
		item = git_strmap_value_at(sc->map, pos);
		goto done;
	}

285 286 287 288
	keylen  = strlen(key);
	itemlen = sc->item_path_offset + keylen + 1;
	itemlen = (itemlen + 7) & ~7;

289
	if ((item = git_pool_mallocz(&sc->pool, (uint32_t)itemlen)) == NULL) {
290
		/* don't use GITERR_CHECK_ALLOC b/c of lock */
291 292 293
		error = -1;
		goto done;
	}
294 295 296 297 298 299 300 301

	/* one strange thing is that even if the vector or hash table insert
	 * fail, there is no way to free the pool item so we just abandon it
	 */

	item_key = ((char *)item) + sc->item_path_offset;
	memcpy(item_key, key, keylen);

302
	pos = git_strmap_put(sc->map, item_key, &error);
303 304 305 306
	if (error < 0)
		goto done;

	if (!error)
307 308
		git_strmap_set_key_at(sc->map, pos, item_key);
	git_strmap_set_value_at(sc->map, pos, item);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

	error = git_vector_insert(&sc->items, item);
	if (error < 0)
		git_strmap_delete_at(sc->map, pos);

done:
	if (out)
		*out = !error ? item : NULL;
	return error;
}

/* lookup item by key */
void *git_sortedcache_lookup(const git_sortedcache *sc, const char *key)
{
	khiter_t pos = git_strmap_lookup_index(sc->map, key);
	if (git_strmap_valid_index(sc->map, pos))
		return git_strmap_value_at(sc->map, pos);
	return NULL;
}

/* find out how many items are in the cache */
size_t git_sortedcache_entrycount(const git_sortedcache *sc)
{
	return git_vector_length(&sc->items);
}

/* lookup item by index */
336
void *git_sortedcache_entry(git_sortedcache *sc, size_t pos)
337
{
338
	/* make sure the items are sorted so this gets the correct item */
339
	if (!git_vector_is_sorted(&sc->items))
340 341
		git_vector_sort(&sc->items);

342 343
	return git_vector_get(&sc->items, pos);
}
344

345
/* helper struct so bsearch callback can know offset + key value for cmp */
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
struct sortedcache_magic_key {
	size_t offset;
	const char *key;
};

static int sortedcache_magic_cmp(const void *key, const void *value)
{
	const struct sortedcache_magic_key *magic = key;
	const char *value_key = ((const char *)value) + magic->offset;
	return strcmp(magic->key, value_key);
}

/* lookup index of item by key */
int git_sortedcache_lookup_index(
	size_t *out, git_sortedcache *sc, const char *key)
{
	struct sortedcache_magic_key magic;

	magic.offset = sc->item_path_offset;
	magic.key    = key;

	return git_vector_bsearch2(out, &sc->items, sortedcache_magic_cmp, &magic);
}

/* remove entry from cache */
371
int git_sortedcache_remove(git_sortedcache *sc, size_t pos)
372 373 374 375 376 377 378 379 380
{
	char *item;
	khiter_t mappos;

	/* because of pool allocation, this can't actually remove the item,
	 * but we can remove it from the items vector and the hash table.
	 */

	if ((item = git_vector_get(&sc->items, pos)) == NULL) {
381
		giterr_set(GITERR_INVALID, "removing item out of range");
382
		return GIT_ENOTFOUND;
383 384 385 386 387 388 389 390 391 392
	}

	(void)git_vector_remove(&sc->items, pos);

	mappos = git_strmap_lookup_index(sc->map, item + sc->item_path_offset);
	git_strmap_delete_at(sc->map, mappos);

	if (sc->free_item)
		sc->free_item(sc->free_item_payload, item);

393
	return 0;
394 395
}