Commit c2f8b215 by Carlos Martín Nieto

index: write out the tree cache extension

Keeping the cache around after read-tree is only one part of the
optimisation opportunities. In order to share the cache between program
instances, we need to write the TREE extension to the index.

Do so, taking the opportunity to rename 'entries' to 'entry_count' to
match the name given in the format description. The included test is
rather trivial, but works as a sanity check.
parent 46bb0067
...@@ -2114,16 +2114,13 @@ static int write_entries(git_index *index, git_filebuf *file) ...@@ -2114,16 +2114,13 @@ static int write_entries(git_index *index, git_filebuf *file)
static int write_extension(git_filebuf *file, struct index_extension *header, git_buf *data) static int write_extension(git_filebuf *file, struct index_extension *header, git_buf *data)
{ {
struct index_extension ondisk; struct index_extension ondisk;
int error = 0;
memset(&ondisk, 0x0, sizeof(struct index_extension)); memset(&ondisk, 0x0, sizeof(struct index_extension));
memcpy(&ondisk, header, 4); memcpy(&ondisk, header, 4);
ondisk.extension_size = htonl(header->extension_size); ondisk.extension_size = htonl(header->extension_size);
if ((error = git_filebuf_write(file, &ondisk, sizeof(struct index_extension))) == 0) git_filebuf_write(file, &ondisk, sizeof(struct index_extension));
error = git_filebuf_write(file, data->ptr, data->size); return git_filebuf_write(file, data->ptr, data->size);
return error;
} }
static int create_name_extension_data(git_buf *name_buf, git_index_name_entry *conflict_name) static int create_name_extension_data(git_buf *name_buf, git_index_name_entry *conflict_name)
...@@ -2229,6 +2226,29 @@ done: ...@@ -2229,6 +2226,29 @@ done:
return error; return error;
} }
static int write_tree_extension(git_index *index, git_filebuf *file)
{
struct index_extension extension;
git_buf buf = GIT_BUF_INIT;
int error;
if (index->tree == NULL)
return 0;
if ((error = git_tree_cache_write(&buf, index->tree)) < 0)
return error;
memset(&extension, 0x0, sizeof(struct index_extension));
memcpy(&extension.signature, INDEX_EXT_TREECACHE_SIG, 4);
extension.extension_size = (uint32_t)buf.size;
error = write_extension(file, &extension, &buf);
git_buf_free(&buf);
return error;
}
static int write_index(git_index *index, git_filebuf *file) static int write_index(git_index *index, git_filebuf *file)
{ {
git_oid hash_final; git_oid hash_final;
...@@ -2251,7 +2271,9 @@ static int write_index(git_index *index, git_filebuf *file) ...@@ -2251,7 +2271,9 @@ static int write_index(git_index *index, git_filebuf *file)
if (write_entries(index, file) < 0) if (write_entries(index, file) < 0)
return -1; return -1;
/* TODO: write tree cache extension */ /* write the tree cache extension */
if (index->tree != NULL && write_tree_extension(index, file) < 0)
return -1;
/* write the rename conflict extension */ /* write the rename conflict extension */
if (index->names.length > 0 && write_name_extension(index, file) < 0) if (index->names.length > 0 && write_name_extension(index, file) < 0)
......
...@@ -31,7 +31,7 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path) ...@@ -31,7 +31,7 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
if (tree == NULL) if (tree == NULL)
return; return;
tree->entries = -1; tree->entry_count = -1;
while (ptr != NULL) { while (ptr != NULL) {
end = strchr(ptr, '/'); end = strchr(ptr, '/');
...@@ -43,7 +43,7 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path) ...@@ -43,7 +43,7 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
if (tree == NULL) /* We don't have that tree */ if (tree == NULL) /* We don't have that tree */
return; return;
tree->entries = -1; tree->entry_count = -1;
ptr = end + 1; ptr = end + 1;
} }
} }
...@@ -93,7 +93,7 @@ static int read_tree_internal(git_tree_cache **out, ...@@ -93,7 +93,7 @@ static int read_tree_internal(git_tree_cache **out,
if (git__strtol32(&count, buffer, &buffer, 10) < 0) if (git__strtol32(&count, buffer, &buffer, 10) < 0)
goto corrupted; goto corrupted;
tree->entries = count; tree->entry_count = count;
if (*buffer != ' ' || ++buffer >= buffer_end) if (*buffer != ' ' || ++buffer >= buffer_end)
goto corrupted; goto corrupted;
...@@ -108,7 +108,7 @@ static int read_tree_internal(git_tree_cache **out, ...@@ -108,7 +108,7 @@ static int read_tree_internal(git_tree_cache **out,
goto corrupted; goto corrupted;
/* The SHA1 is only there if it's not invalidated */ /* The SHA1 is only there if it's not invalidated */
if (tree->entries >= 0) { if (tree->entry_count >= 0) {
/* 160-bit SHA-1 for this tree and it's children */ /* 160-bit SHA-1 for this tree and it's children */
if (buffer + GIT_OID_RAWSZ > buffer_end) if (buffer + GIT_OID_RAWSZ > buffer_end)
goto corrupted; goto corrupted;
...@@ -244,3 +244,55 @@ int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool) ...@@ -244,3 +244,55 @@ int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool)
*out = tree; *out = tree;
return 0; return 0;
} }
/**
* Recursively recalculate the total entry count, which we need to
* write out to the index
*/
static void recount_entries(git_tree_cache *tree)
{
size_t i;
ssize_t entry_count;
git_tree_cache *child;
for (i = 0; i < tree->children_count; i++)
recount_entries(tree->children[i]);
if (tree->entry_count == -1)
return;
entry_count = 0;
for (i = 0; i < tree->children_count; i++) {
child = tree->children[i];
if (child->entry_count == -1)
continue;
entry_count += tree->children[i]->children_count;
}
tree->entry_count = entry_count;
}
static void write_tree(git_buf *out, git_tree_cache *tree)
{
size_t i;
git_buf_printf(out, "%s%c%zd %"PRIuZ"\n", tree->name, 0, tree->entry_count, tree->children_count);
if (tree->entry_count == -1)
return;
git_buf_put(out, (const char *) &tree->oid, GIT_OID_RAWSZ);
for (i = 0; i < tree->children_count; i++)
write_tree(out, tree->children[i]);
}
int git_tree_cache_write(git_buf *out, git_tree_cache *tree)
{
recount_entries(tree);
write_tree(out, tree);
return git_buf_oom(out) ? -1 : 0;
}
...@@ -10,18 +10,20 @@ ...@@ -10,18 +10,20 @@
#include "common.h" #include "common.h"
#include "pool.h" #include "pool.h"
#include "buffer.h"
#include "git2/oid.h" #include "git2/oid.h"
typedef struct git_tree_cache { typedef struct git_tree_cache {
struct git_tree_cache **children; struct git_tree_cache **children;
size_t children_count; size_t children_count;
ssize_t entries; ssize_t entry_count;
git_oid oid; git_oid oid;
size_t namelen; size_t namelen;
char name[GIT_FLEX_ARRAY]; char name[GIT_FLEX_ARRAY];
} git_tree_cache; } git_tree_cache;
int git_tree_cache_write(git_buf *out, git_tree_cache *tree);
int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_pool *pool); int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_pool *pool);
void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path); void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path);
const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path); const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path);
......
...@@ -488,7 +488,7 @@ static int write_tree( ...@@ -488,7 +488,7 @@ static int write_tree(
const git_tree_cache *cache; const git_tree_cache *cache;
cache = git_tree_cache_get(index->tree, dirname); cache = git_tree_cache_get(index->tree, dirname);
if (cache != NULL && cache->entries >= 0){ if (cache != NULL && cache->entry_count >= 0){
git_oid_cpy(oid, &cache->oid); git_oid_cpy(oid, &cache->oid);
return (int)find_next_dir(dirname, index, start); return (int)find_next_dir(dirname, index, start);
} }
...@@ -589,7 +589,7 @@ int git_tree__write_index( ...@@ -589,7 +589,7 @@ int git_tree__write_index(
return GIT_EUNMERGED; return GIT_EUNMERGED;
} }
if (index->tree != NULL && index->tree->entries >= 0) { if (index->tree != NULL && index->tree->entry_count >= 0) {
git_oid_cpy(oid, &index->tree->oid); git_oid_cpy(oid, &index->tree->oid);
return 0; return 0;
} }
......
...@@ -16,6 +16,79 @@ void test_index_cache__cleanup(void) ...@@ -16,6 +16,79 @@ void test_index_cache__cleanup(void)
g_repo = NULL; g_repo = NULL;
} }
void test_index_cache__write_extension_at_root(void)
{
git_index *index;
git_tree *tree;
git_oid id;
const char *tree_id_str = "45dd856fdd4d89b884c340ba0e047752d9b085d6";
const char *index_file = "index-tree";
cl_git_pass(git_index_open(&index, index_file));
cl_assert(index->tree == NULL);
cl_git_pass(git_oid_fromstr(&id, tree_id_str));
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_index_read_tree(index, tree));
git_tree_free(tree);
cl_assert(index->tree);
cl_git_pass(git_index_write(index));
git_index_free(index);
cl_git_pass(git_index_open(&index, index_file));
cl_assert(index->tree);
cl_assert_equal_i(0, index->tree->entry_count);
cl_assert_equal_i(0, index->tree->children_count);
cl_assert(git_oid_equal(&id, &index->tree->oid));
cl_git_pass(p_unlink(index_file));
git_index_free(index);
}
void test_index_cache__write_extension_invalidated_root(void)
{
git_index *index;
git_tree *tree;
git_oid id;
const char *tree_id_str = "45dd856fdd4d89b884c340ba0e047752d9b085d6";
const char *index_file = "index-tree-invalidated";
git_index_entry entry;
cl_git_pass(git_index_open(&index, index_file));
cl_assert(index->tree == NULL);
cl_git_pass(git_oid_fromstr(&id, tree_id_str));
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_index_read_tree(index, tree));
git_tree_free(tree);
cl_assert(index->tree);
memset(&entry, 0x0, sizeof(git_index_entry));
git_oid_cpy(&entry.id, &git_index_get_byindex(index, 0)->id);
entry.mode = GIT_FILEMODE_BLOB;
entry.path = "some-new-file.txt";
cl_git_pass(git_index_add(index, &entry));
cl_assert_equal_i(-1, index->tree->entry_count);
cl_git_pass(git_index_write(index));
git_index_free(index);
cl_git_pass(git_index_open(&index, index_file));
cl_assert(index->tree);
cl_assert_equal_i(-1, index->tree->entry_count);
cl_assert_equal_i(0, index->tree->children_count);
cl_assert(git_oid_cmp(&id, &index->tree->oid));
cl_git_pass(p_unlink(index_file));
git_index_free(index);
}
void test_index_cache__read_tree_no_children(void) void test_index_cache__read_tree_no_children(void)
{ {
git_index *index; git_index *index;
...@@ -33,7 +106,7 @@ void test_index_cache__read_tree_no_children(void) ...@@ -33,7 +106,7 @@ void test_index_cache__read_tree_no_children(void)
cl_assert(index->tree); cl_assert(index->tree);
cl_assert(git_oid_equal(&id, &index->tree->oid)); cl_assert(git_oid_equal(&id, &index->tree->oid));
cl_assert_equal_i(0, index->tree->children_count); cl_assert_equal_i(0, index->tree->children_count);
cl_assert_equal_i(0, index->tree->entries); /* 0 is a placeholder here */ cl_assert_equal_i(0, index->tree->entry_count); /* 0 is a placeholder here */
memset(&entry, 0x0, sizeof(git_index_entry)); memset(&entry, 0x0, sizeof(git_index_entry));
entry.path = "new.txt"; entry.path = "new.txt";
...@@ -41,7 +114,7 @@ void test_index_cache__read_tree_no_children(void) ...@@ -41,7 +114,7 @@ void test_index_cache__read_tree_no_children(void)
git_oid_fromstr(&entry.id, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057"); git_oid_fromstr(&entry.id, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
cl_git_pass(git_index_add(index, &entry)); cl_git_pass(git_index_add(index, &entry));
cl_assert_equal_i(-1, index->tree->entries); cl_assert_equal_i(-1, index->tree->entry_count);
git_index_free(index); git_index_free(index);
} }
...@@ -92,19 +165,19 @@ void test_index_cache__read_tree_children(void) ...@@ -92,19 +165,19 @@ void test_index_cache__read_tree_children(void)
git_oid_fromstr(&entry.id, "45b983be36b73c0788dc9cbcb76cbb80fc7bb058"); git_oid_fromstr(&entry.id, "45b983be36b73c0788dc9cbcb76cbb80fc7bb058");
cl_git_pass(git_index_add(index, &entry)); cl_git_pass(git_index_add(index, &entry));
cl_assert_equal_i(-1, index->tree->entries); cl_assert_equal_i(-1, index->tree->entry_count);
cache = git_tree_cache_get(index->tree, "subdir"); cache = git_tree_cache_get(index->tree, "subdir");
cl_assert(cache); cl_assert(cache);
cl_assert_equal_i(-1, cache->entries); cl_assert_equal_i(-1, cache->entry_count);
cache = git_tree_cache_get(index->tree, "subdir/even-deeper"); cache = git_tree_cache_get(index->tree, "subdir/even-deeper");
cl_assert(cache); cl_assert(cache);
cl_assert_equal_i(0, cache->entries); cl_assert_equal_i(0, cache->entry_count);
cache = git_tree_cache_get(index->tree, "subdir2"); cache = git_tree_cache_get(index->tree, "subdir2");
cl_assert(cache); cl_assert(cache);
cl_assert_equal_i(0, cache->entries); cl_assert_equal_i(0, cache->entry_count);
git_index_free(index); git_index_free(index);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment