From: Thomas Gummerer <t.gummerer@gmail.com>
To: git@vger.kernel.org
Cc: trast@inf.ethz.ch, mhagger@alum.mit.edu, gitster@pobox.com,
pclouds@gmail.com, robin.rosenberg@dewire.com,
sunshine@sunshineco.com, ramsay@ramsay1.demon.co.uk,
t.gummerer@gmail.com
Subject: [PATCH v3 09/24] read-cache: add index reading api
Date: Sun, 18 Aug 2013 21:41:58 +0200 [thread overview]
Message-ID: <1376854933-31241-10-git-send-email-t.gummerer@gmail.com> (raw)
In-Reply-To: <1376854933-31241-1-git-send-email-t.gummerer@gmail.com>
Add an api for access to the index file. Currently there is only a very
basic api for accessing the index file, which only allows a full read of
the index, and lets the users of the data filter it. The new index api
gives the users the possibility to use only part of the index and
provides functions for iterating over and accessing cache entries.
This simplifies future improvements to the in-memory format, as changes
will be concentrated on one file, instead of the whole git source code.
Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
cache.h | 41 ++++++++++++++++++++++++++++++++++++++++-
read-cache-v2.c | 10 ++++++++--
read-cache.c | 47 +++++++++++++++++++++++++++++++++++++++++++----
read-cache.h | 3 ++-
4 files changed, 93 insertions(+), 8 deletions(-)
diff --git a/cache.h b/cache.h
index d4dae21..da224c9 100644
--- a/cache.h
+++ b/cache.h
@@ -127,7 +127,7 @@ struct cache_entry {
unsigned int ce_flags;
unsigned int ce_namelen;
unsigned char sha1[20];
- struct cache_entry *next;
+ struct cache_entry *next; /* used by name_hash */
char name[FLEX_ARRAY]; /* more */
};
@@ -260,6 +260,29 @@ static inline unsigned int canon_mode(unsigned int mode)
#define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)
+/*
+ * Options by which the index should be filtered when read partially.
+ *
+ * pathspec: The pathspec which the index entries have to match
+ * seen: Used to return the seen parameter from match_pathspec()
+ * max_prefix_len: The common prefix length of the pathspecs
+ *
+ * read_staged: used to indicate if the conflicted entries (entries
+ * with a stage) should be included
+ * read_cache_tree: used to indicate if the cache-tree should be read
+ * read_resolve_undo: used to indicate if the resolve undo data should
+ * be read
+ */
+struct filter_opts {
+ const struct pathspec *pathspec;
+ char *seen;
+ int max_prefix_len;
+
+ int read_staged;
+ int read_cache_tree;
+ int read_resolve_undo;
+};
+
struct index_state {
struct cache_entry **cache;
unsigned int version;
@@ -272,6 +295,7 @@ struct index_state {
struct hash_table name_hash;
struct hash_table dir_hash;
struct index_ops *ops;
+ struct filter_opts *filter_opts;
};
extern struct index_state the_index;
@@ -315,6 +339,12 @@ extern void free_name_hash(struct index_state *istate);
#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
#define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
#define read_blob_data_from_cache(path, sz) read_blob_data_from_index(&the_index, (path), (sz))
+
+/* index api */
+#define read_cache_filtered(opts) read_index_filtered(&the_index, (opts))
+#define read_cache_filtered_from(path, opts) read_index_filtered_from(&the_index, (path), (opts))
+#define for_each_cache_entry(fn, cb_data) \
+ for_each_index_entry(&the_index, (fn), (cb_data))
#endif
enum object_type {
@@ -448,6 +478,15 @@ extern void sanitize_stdfds(void);
} \
} while (0)
+/* index api */
+extern int read_index_filtered(struct index_state *, struct filter_opts *opts);
+extern int read_index_filtered_from(struct index_state *, const char *path, struct filter_opts *opts);
+
+typedef int each_cache_entry_fn(struct cache_entry *ce, void *);
+extern int for_each_index_entry(struct index_state *istate,
+ each_cache_entry_fn, void *);
+
+
/* Initialize and use the cache information */
extern void initialize_index(struct index_state *istate, int version);
extern void change_index_version(struct index_state *istate, int version);
diff --git a/read-cache-v2.c b/read-cache-v2.c
index 070d468..63be074 100644
--- a/read-cache-v2.c
+++ b/read-cache-v2.c
@@ -3,6 +3,7 @@
#include "resolve-undo.h"
#include "cache-tree.h"
#include "varint.h"
+#include "dir.h"
/* Mask for the name length in ce_flags in the on-disk index */
#define CE_NAMEMASK (0x0fff)
@@ -202,8 +203,14 @@ static int read_index_extension(struct index_state *istate,
return 0;
}
+/*
+ * The performance is the same if we read the whole index or only
+ * part of it, therefore we always read the whole index to avoid
+ * having to re-read it later. The filter_opts will determine
+ * what part of the index is used when retrieving the cache-entries.
+ */
static int read_index_v2(struct index_state *istate, void *mmap,
- unsigned long mmap_size)
+ unsigned long mmap_size, struct filter_opts *opts)
{
int i;
unsigned long src_offset;
@@ -229,7 +236,6 @@ static int read_index_v2(struct index_state *istate, void *mmap,
disk_ce = (struct ondisk_cache_entry *)((char *)mmap + src_offset);
ce = create_from_disk(disk_ce, &consumed, previous_name);
set_index_entry(istate, i, ce);
-
src_offset += consumed;
}
strbuf_release(&previous_name_buf);
diff --git a/read-cache.c b/read-cache.c
index 2d12601..38b9a04 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1290,11 +1290,41 @@ static int verify_hdr_version(struct index_state *istate,
int read_index(struct index_state *istate)
{
- return read_index_from(istate, get_index_file());
+ return read_index_filtered_from(istate, get_index_file(), NULL);
}
-/* remember to discard_cache() before reading a different cache! */
-int read_index_from(struct index_state *istate, const char *path)
+int read_index_filtered(struct index_state *istate, struct filter_opts *opts)
+{
+ return read_index_filtered_from(istate, get_index_file(), opts);
+}
+
+/*
+ * Execute fn for each index entry which is currently in istate. Data
+ * can be given to the function using the cb_data parameter.
+ */
+int for_each_index_entry(struct index_state *istate, each_cache_entry_fn fn, void *cb_data)
+{
+ int i, ret = 0;
+ struct filter_opts *opts = istate->filter_opts;
+
+ for (i = 0; i < istate->cache_nr; i++) {
+ struct cache_entry *ce = istate->cache[i];
+
+ if (opts && !opts->read_staged && ce_stage(ce))
+ continue;
+
+ if (opts && !match_pathspec_depth(opts->pathspec, ce->name, ce_namelen(ce),
+ opts->max_prefix_len, opts->seen))
+ continue;
+
+ if ((ret = fn(istate->cache[i], cb_data)))
+ break;
+ }
+ return ret;
+}
+
+int read_index_filtered_from(struct index_state *istate, const char *path,
+ struct filter_opts *opts)
{
int fd, err, i;
struct stat_validity sv;
@@ -1309,6 +1339,7 @@ int read_index_from(struct index_state *istate, const char *path)
errno = ENOENT;
istate->timestamp.sec = 0;
istate->timestamp.nsec = 0;
+ istate->filter_opts = opts;
sv.sd = NULL;
for (i = 0; i < 50; i++) {
err = 0;
@@ -1339,7 +1370,7 @@ int read_index_from(struct index_state *istate, const char *path)
if (istate->ops->verify_hdr(mmap, mmap_size) < 0)
err = 1;
- if (istate->ops->read_index(istate, mmap, mmap_size) < 0)
+ if (istate->ops->read_index(istate, mmap, mmap_size, opts) < 0)
err = 1;
istate->timestamp.sec = sv.sd->sd_mtime.sec;
istate->timestamp.nsec = sv.sd->sd_mtime.nsec;
@@ -1352,6 +1383,13 @@ int read_index_from(struct index_state *istate, const char *path)
die("index file corrupt");
}
+
+/* remember to discard_cache() before reading a different cache! */
+int read_index_from(struct index_state *istate, const char *path)
+{
+ return read_index_filtered_from(istate, path, NULL);
+}
+
int is_index_unborn(struct index_state *istate)
{
return (!istate->cache_nr && !istate->timestamp.sec);
@@ -1376,6 +1414,7 @@ int discard_index(struct index_state *istate)
istate->cache = NULL;
istate->cache_alloc = 0;
istate->ops = NULL;
+ istate->filter_opts = NULL;
return 0;
}
diff --git a/read-cache.h b/read-cache.h
index d8debb8..644b199 100644
--- a/read-cache.h
+++ b/read-cache.h
@@ -25,7 +25,8 @@ struct cache_header {
struct index_ops {
int (*match_stat_basic)(const struct cache_entry *ce, struct stat *st, int changed);
int (*verify_hdr)(void *mmap, unsigned long size);
- int (*read_index)(struct index_state *istate, void *mmap, unsigned long mmap_size);
+ int (*read_index)(struct index_state *istate, void *mmap, unsigned long mmap_size,
+ struct filter_opts *opts);
int (*write_index)(struct index_state *istate, int newfd);
};
--
1.8.3.4.1231.g9fbf354.dirty
next prev parent reply other threads:[~2013-08-18 19:48 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-18 19:41 [PATCH v3 00/24] Index-v5 Thomas Gummerer
2013-08-18 19:41 ` [PATCH v3 01/24] t2104: Don't fail for index versions other than [23] Thomas Gummerer
2013-08-18 19:41 ` [PATCH v3 02/24] read-cache: use fixed width integer types Thomas Gummerer
2013-08-18 20:21 ` Eric Sunshine
2013-08-20 19:30 ` Junio C Hamano
2013-08-21 3:05 ` Thomas Gummerer
2013-08-18 19:41 ` [PATCH v3 03/24] read-cache: split index file version specific functionality Thomas Gummerer
2013-08-18 19:41 ` [PATCH v3 04/24] read-cache: clear version in discard_index() Thomas Gummerer
2013-08-20 19:34 ` Junio C Hamano
2013-08-21 3:06 ` Thomas Gummerer
2013-08-18 19:41 ` [PATCH v3 05/24] read-cache: move index v2 specific functions to their own file Thomas Gummerer
2013-08-18 19:41 ` [PATCH v3 06/24] read-cache: Don't compare uid, gid and ino on cygwin Thomas Gummerer
2013-08-18 22:34 ` Ramsay Jones
2013-08-20 8:36 ` Thomas Gummerer
2013-08-18 19:41 ` [PATCH v3 07/24] read-cache: Re-read index if index file changed Thomas Gummerer
2013-08-18 19:41 ` [PATCH v3 08/24] add documentation for the index api Thomas Gummerer
2013-08-18 20:50 ` Eric Sunshine
2013-08-18 19:41 ` Thomas Gummerer [this message]
2013-08-18 19:41 ` [PATCH v3 10/24] make sure partially read index is not changed Thomas Gummerer
2013-08-18 21:06 ` Eric Sunshine
2013-08-20 8:46 ` Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 11/24] grep.c: use index api Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 12/24] ls-files.c: " Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 13/24] documentation: add documentation of the index-v5 file format Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 14/24] read-cache: make in-memory format aware of stat_crc Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 15/24] read-cache: read index-v5 Thomas Gummerer
2013-08-19 1:57 ` Eric Sunshine
2013-08-20 14:01 ` Duy Nguyen
2013-08-20 20:59 ` Thomas Gummerer
2013-08-21 0:44 ` Duy Nguyen
2013-08-20 14:16 ` Duy Nguyen
2013-08-20 21:13 ` Thomas Gummerer
2013-08-23 23:52 ` Duy Nguyen
2013-08-18 19:42 ` [PATCH v3 16/24] read-cache: read resolve-undo data Thomas Gummerer
2013-08-19 1:59 ` Eric Sunshine
2013-08-18 19:42 ` [PATCH v3 17/24] read-cache: read cache-tree in index-v5 Thomas Gummerer
2013-08-24 0:09 ` Duy Nguyen
2013-11-25 15:41 ` Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 18/24] read-cache: write index-v5 Thomas Gummerer
2013-08-24 3:58 ` Duy Nguyen
2013-11-25 15:37 ` Thomas Gummerer
2013-08-24 4:07 ` Duy Nguyen
2013-08-24 9:56 ` Duy Nguyen
2013-08-18 19:42 ` [PATCH v3 19/24] read-cache: write index-v5 cache-tree data Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 20/24] read-cache: write resolve-undo data for index-v5 Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 21/24] update-index.c: rewrite index when index-version is given Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 22/24] p0003-index.sh: add perf test for the index formats Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 23/24] introduce GIT_INDEX_VERSION environment variable Thomas Gummerer
2013-08-21 0:57 ` Duy Nguyen
2013-08-21 4:01 ` Thomas Gummerer
2013-08-18 19:42 ` [PATCH v3 24/24] test-lib: allow setting the index format version Thomas Gummerer
2013-08-24 4:16 ` [PATCH v3 00/24] Index-v5 Duy Nguyen
2013-08-25 3:07 ` Junio C Hamano
2013-08-25 4:40 ` Duy Nguyen
2013-08-31 5:23 ` Thomas Gummerer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1376854933-31241-10-git-send-email-t.gummerer@gmail.com \
--to=t.gummerer@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=mhagger@alum.mit.edu \
--cc=pclouds@gmail.com \
--cc=ramsay@ramsay1.demon.co.uk \
--cc=robin.rosenberg@dewire.com \
--cc=sunshine@sunshineco.com \
--cc=trast@inf.ethz.ch \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).