Blame src/sortedcache.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_sorted_cache_h__
Packit Service 20376f
#define INCLUDE_sorted_cache_h__
Packit Service 20376f
Packit Service 20376f
#include "util.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "vector.h"
Packit Service 20376f
#include "thread-utils.h"
Packit Service 20376f
#include "pool.h"
Packit Service 20376f
#include "strmap.h"
Packit Service 20376f
Packit Service 20376f
#include <stddef.h>
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * The purpose of this data structure is to cache the parsed contents of a
Packit Service 20376f
 * file (a.k.a. the backing file) where each item in the file can be
Packit Service 20376f
 * identified by a key string and you want to both look them up by name
Packit Service 20376f
 * and traverse them in sorted order.  Each item is assumed to itself end
Packit Service 20376f
 * in a GIT_FLEX_ARRAY.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
typedef void (*git_sortedcache_free_item_fn)(void *payload, void *item);
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	git_refcount rc;
Packit Service 20376f
	git_rwlock   lock;
Packit Service 20376f
	size_t       item_path_offset;
Packit Service 20376f
	git_sortedcache_free_item_fn free_item;
Packit Service 20376f
	void         *free_item_payload;
Packit Service 20376f
	git_pool     pool;
Packit Service 20376f
	git_vector   items;
Packit Service 20376f
	git_strmap   *map;
Packit Service 20376f
	git_futils_filestamp stamp;
Packit Service 20376f
	char         path[GIT_FLEX_ARRAY];
Packit Service 20376f
} git_sortedcache;
Packit Service 20376f
Packit Service 20376f
/* Create a new sortedcache
Packit Service 20376f
 *
Packit Service 20376f
 * Even though every sortedcache stores items with a GIT_FLEX_ARRAY at
Packit Service 20376f
 * the end containing their key string, you have to provide the item_cmp
Packit Service 20376f
 * sorting function because the sorting function doesn't get a payload
Packit Service 20376f
 * and therefore can't know the offset to the item key string. :-(
Packit Service 20376f
 *
Packit Service 20376f
 * @param out The allocated git_sortedcache
Packit Service 20376f
 * @param item_path_offset Offset to the GIT_FLEX_ARRAY item key in the
Packit Service 20376f
 *        struct - use offsetof(struct mine, key-field) to get this
Packit Service 20376f
 * @param free_item Optional callback to free each item
Packit Service 20376f
 * @param free_item_payload Optional payload passed to free_item callback
Packit Service 20376f
 * @param item_cmp Compare the keys of two items
Packit Service 20376f
 * @param path The path to the backing store file for this cache; this
Packit Service 20376f
 *        may be NULL.  The cache makes it easy to load this and check
Packit Service 20376f
 *        if it has been modified since the last load and/or write.
Packit Service 20376f
 */
Packit Service 20376f
int git_sortedcache_new(
Packit Service 20376f
	git_sortedcache **out,
Packit Service 20376f
	size_t item_path_offset, /* use offsetof(struct, path-field) macro */
Packit Service 20376f
	git_sortedcache_free_item_fn free_item,
Packit Service 20376f
	void *free_item_payload,
Packit Service 20376f
	git_vector_cmp item_cmp,
Packit Service 20376f
	const char *path);
Packit Service 20376f
Packit Service 20376f
/* Copy a sorted cache
Packit Service 20376f
 *
Packit Service 20376f
 * - `copy_item` can be NULL to just use memcpy
Packit Service 20376f
 * - if `lock`, grabs read lock on `src` during copy and releases after
Packit Service 20376f
 */
Packit Service 20376f
int git_sortedcache_copy(
Packit Service 20376f
	git_sortedcache **out,
Packit Service 20376f
	git_sortedcache *src,
Packit Service 20376f
	bool lock,
Packit Service 20376f
	int (*copy_item)(void *payload, void *tgt_item, void *src_item),
Packit Service 20376f
	void *payload);
Packit Service 20376f
Packit Service 20376f
/* Free sorted cache (first calling `free_item` callbacks)
Packit Service 20376f
 *
Packit Service 20376f
 * Don't call on a locked collection - it may acquire a write lock
Packit Service 20376f
 */
Packit Service 20376f
void git_sortedcache_free(git_sortedcache *sc);
Packit Service 20376f
Packit Service 20376f
/* Increment reference count - balance with call to free */
Packit Service 20376f
void git_sortedcache_incref(git_sortedcache *sc);
Packit Service 20376f
Packit Service 20376f
/* Get the pathname associated with this cache at creation time */
Packit Service 20376f
const char *git_sortedcache_path(git_sortedcache *sc);
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * CACHE WRITE FUNCTIONS
Packit Service 20376f
 *
Packit Service 20376f
 * The following functions require you to have a writer lock to make the
Packit Service 20376f
 * modification.  Some of the functions take a `wlock` parameter and
Packit Service 20376f
 * will optionally lock and unlock for you if that is passed as true.
Packit Service 20376f
 *
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
/* Lock sortedcache for write */
Packit Service 20376f
int git_sortedcache_wlock(git_sortedcache *sc);
Packit Service 20376f
Packit Service 20376f
/* Unlock sorted cache when done with write */
Packit Service 20376f
void git_sortedcache_wunlock(git_sortedcache *sc);
Packit Service 20376f
Packit Service 20376f
/* Lock cache and load backing file into a buffer.
Packit Service 20376f
 *
Packit Service 20376f
 * This grabs a write lock on the cache then looks at the modification
Packit Service 20376f
 * time and size of the file on disk.
Packit Service 20376f
 *
Packit Service 20376f
 * If the file appears to have changed, this loads the file contents into
Packit Service 20376f
 * the buffer and returns a positive value leaving the cache locked - the
Packit Service 20376f
 * caller should parse the file content, update the cache as needed, then
Packit Service 20376f
 * release the lock.  NOTE: In this case, the caller MUST unlock the cache.
Packit Service 20376f
 *
Packit Service 20376f
 * If the file appears to be unchanged, then this automatically releases
Packit Service 20376f
 * the lock on the cache, clears the buffer, and returns 0.
Packit Service 20376f
 *
Packit Service 20376f
 * @return 0 if up-to-date, 1 if out-of-date, <0 on error
Packit Service 20376f
 */
Packit Service 20376f
int git_sortedcache_lockandload(git_sortedcache *sc, git_buf *buf);
Packit Service 20376f
Packit Service 20376f
/* Refresh file timestamp after write completes
Packit Service 20376f
 * You should already be holding the write lock when you call this.
Packit Service 20376f
 */
Packit Service 20376f
void git_sortedcache_updated(git_sortedcache *sc);
Packit Service 20376f
Packit Service 20376f
/* Release all items in sorted cache
Packit Service 20376f
 *
Packit Service 20376f
 * If `wlock` is true, grabs write lock and releases when done, otherwise
Packit Service 20376f
 * you should already be holding a write lock when you call this.
Packit Service 20376f
 */
Packit Service 20376f
int git_sortedcache_clear(git_sortedcache *sc, bool wlock);
Packit Service 20376f
Packit Service 20376f
/* Find and/or insert item, returning pointer to item data.
Packit Service 20376f
 * You should already be holding the write lock when you call this.
Packit Service 20376f
 */
Packit Service 20376f
int git_sortedcache_upsert(
Packit Service 20376f
	void **out, git_sortedcache *sc, const char *key);
Packit Service 20376f
Packit Service 20376f
/* Removes entry at pos from cache
Packit Service 20376f
 * You should already be holding the write lock when you call this.
Packit Service 20376f
 */
Packit Service 20376f
int git_sortedcache_remove(git_sortedcache *sc, size_t pos);
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * CACHE READ FUNCTIONS
Packit Service 20376f
 *
Packit Service 20376f
 * The following functions access items in the cache.  To prevent the
Packit Service 20376f
 * results from being invalidated before they can be used, you should be
Packit Service 20376f
 * holding either a read lock or a write lock when using these functions.
Packit Service 20376f
 *
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
/* Lock sortedcache for read */
Packit Service 20376f
int git_sortedcache_rlock(git_sortedcache *sc);
Packit Service 20376f
Packit Service 20376f
/* Unlock sorted cache when done with read */
Packit Service 20376f
void git_sortedcache_runlock(git_sortedcache *sc);
Packit Service 20376f
Packit Service 20376f
/* Lookup item by key - returns NULL if not found */
Packit Service 20376f
void *git_sortedcache_lookup(const git_sortedcache *sc, const char *key);
Packit Service 20376f
Packit Service 20376f
/* Get how many items are in the cache
Packit Service 20376f
 *
Packit Service 20376f
 * You can call this function without holding a lock, but be aware
Packit Service 20376f
 * that it may change before you use it.
Packit Service 20376f
 */
Packit Service 20376f
size_t git_sortedcache_entrycount(const git_sortedcache *sc);
Packit Service 20376f
Packit Service 20376f
/* Lookup item by index - returns NULL if out of range */
Packit Service 20376f
void *git_sortedcache_entry(git_sortedcache *sc, size_t pos);
Packit Service 20376f
Packit Service 20376f
/* Lookup index of item by key - returns GIT_ENOTFOUND if not found */
Packit Service 20376f
int git_sortedcache_lookup_index(
Packit Service 20376f
	size_t *out, git_sortedcache *sc, const char *key);
Packit Service 20376f
Packit Service 20376f
#endif