summaryrefslogtreecommitdiff
path: root/src/odb_loose.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-07-09 02:10:46 +0200
committerVicent Marti <tanoku@gmail.com>2011-07-09 02:40:16 +0200
commitafeecf4f262b74270368ef8a70c582ea9d5a18e8 (patch)
treec078ee522e3d9f7bf01fe7e85f5baa7f41dacde4 /src/odb_loose.c
parent2fc78e700cc4684c1e5899d7a4a619da1e3e3679 (diff)
downloadlibgit2-afeecf4f262b74270368ef8a70c582ea9d5a18e8.tar.gz
odb: Direct writes are back
DIRECT WRITES ARE BACK AND FASTER THAN EVER. The streaming writer to the ODB was an overkill for the smaller objects like Commit and Tags; most of the streaming logic was taking too long. This commit makes Commits, Tags and Trees to be built-up in memory, and then written to disk in 2 pushes (header + data), instead of streaming everything. This is *always* faster, even for big files (since the git_filebuf class still does streaming writes when the memory cache overflows). This is also a gazillion lines of code smaller, because we don't have to precompute the final size of the object before starting the stream (this was kind of defeating the point of streaming, anyway). Blobs are still written with full streaming instead of loading them in memory, since this is still the fastest way. A new `git_buf` class has been added. It's missing some features, but it'll get there.
Diffstat (limited to 'src/odb_loose.c')
-rw-r--r--src/odb_loose.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 2a2c464c7..a3a4e5b56 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -772,6 +772,49 @@ int loose_backend__stream(git_odb_stream **stream_out, git_odb_backend *_backend
return GIT_SUCCESS;
}
+int loose_backend__write(git_oid *oid, git_odb_backend *_backend, const void *data, size_t len, git_otype type)
+{
+ int error, header_len;
+ char final_path[GIT_PATH_MAX], header[64];
+ git_filebuf fbuf;
+ loose_backend *backend;
+
+ backend = (loose_backend *)_backend;
+
+ /* prepare the header for the file */
+ {
+ header_len = format_object_header(header, sizeof(header), len, type);
+ if (header_len < GIT_SUCCESS)
+ return GIT_EOBJCORRUPTED;
+ }
+
+ git_path_join(final_path, backend->objects_dir, "tmp_object");
+
+ error = git_filebuf_open(&fbuf, final_path,
+ GIT_FILEBUF_HASH_CONTENTS |
+ GIT_FILEBUF_DEFLATE_CONTENTS |
+ GIT_FILEBUF_TEMPORARY);
+
+ if (error < GIT_SUCCESS)
+ return error;
+
+ git_filebuf_write(&fbuf, header, header_len);
+ git_filebuf_write(&fbuf, data, len);
+ git_filebuf_hash(oid, &fbuf);
+
+ if ((error = object_file_name(final_path, sizeof(final_path), backend->objects_dir, oid)) < GIT_SUCCESS)
+ goto cleanup;
+
+ if ((error = git_futils_mkpath2file(final_path)) < GIT_SUCCESS)
+ goto cleanup;
+
+ return git_filebuf_commit_at(&fbuf, final_path);
+
+cleanup:
+ git_filebuf_cleanup(&fbuf);
+ return error;
+}
+
void loose_backend__free(git_odb_backend *_backend)
{
loose_backend *backend;
@@ -800,6 +843,7 @@ int git_odb_backend_loose(git_odb_backend **backend_out, const char *objects_dir
backend->fsync_object_files = 0;
backend->parent.read = &loose_backend__read;
+ backend->parent.write = &loose_backend__write;
backend->parent.read_prefix = &loose_backend__read_prefix;
backend->parent.read_header = &loose_backend__read_header;
backend->parent.writestream = &loose_backend__stream;