summaryrefslogtreecommitdiff
path: root/include/git2
diff options
context:
space:
mode:
authorVicent Martí <vicent@github.com>2013-08-19 02:17:00 -0700
committerVicent Martí <vicent@github.com>2013-08-19 02:17:00 -0700
commit520287f63a86c5d284ff4d23a2d5d61decbb8685 (patch)
tree4a0111fcf75db57c0ad15f4c4c152e4a2b6d5cff /include/git2
parent1c1b4e8a15b059ae8feb61342de0408755d849ec (diff)
parent090a07d29548ce69034b4be6ba043014226893c9 (diff)
downloadlibgit2-520287f63a86c5d284ff4d23a2d5d61decbb8685.tar.gz
Merge pull request #1785 from libgit2/cmn/odb-hash-frontend
odb: move hashing to the frontend for streaming
Diffstat (limited to 'include/git2')
-rw-r--r--include/git2/odb.h49
-rw-r--r--include/git2/odb_backend.h31
-rw-r--r--include/git2/sys/odb_backend.h8
3 files changed, 72 insertions, 16 deletions
diff --git a/include/git2/odb.h b/include/git2/odb.h
index b3e9a57a6..3e93a932c 100644
--- a/include/git2/odb.h
+++ b/include/git2/odb.h
@@ -219,16 +219,9 @@ GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size
* The type and final length of the object must be specified
* when opening the stream.
*
- * The returned stream will be of type `GIT_STREAM_WRONLY` and
- * will have the following methods:
- *
- * - stream->write: write `n` bytes into the stream
- * - stream->finalize_write: close the stream and store the object in
- * the odb
- * - stream->free: free the stream
- *
- * The streaming write won't be effective until `stream->finalize_write`
- * is called and returns without an error
+ * The returned stream will be of type `GIT_STREAM_WRONLY`, and it
+ * won't be effective until `git_odb_stream_finalize_write` is called
+ * and returns without an error
*
* The stream must always be free'd or will leak memory.
*
@@ -243,6 +236,42 @@ GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size
GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, size_t size, git_otype type);
/**
+ * Write to an odb stream
+ *
+ * @param stream the stream
+ * @param buffer the data to write
+ * @param len the buffer's length
+ * @return 0 if the write succeeded; error code otherwise
+ */
+GIT_EXTERN(int) git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len);
+
+/**
+ * Finish writing to an odb stream
+ *
+ * The object will take its final name and will be available to the
+ * odb.
+ *
+ * @param out pointer to store the resulting object's id
+ * @param stream the stream
+ * @return 0 on success; an error code otherwise
+ */
+GIT_EXTERN(int) git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream);
+
+/**
+ * Read from an odb stream
+ *
+ * Most backends don't implement streaming reads
+ */
+GIT_EXTERN(int) git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len);
+
+/**
+ * Free an odb stream
+ *
+ * @param stream the stream to free
+ */
+GIT_EXTERN(void) git_odb_stream_free(git_odb_stream *stream);
+
+/**
* Open a stream to read an object from the ODB
*
* Note that most backends do *not* support streaming reads
diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h
index af1e3e5b9..dca499583 100644
--- a/include/git2/odb_backend.h
+++ b/include/git2/odb_backend.h
@@ -65,14 +65,41 @@ typedef enum {
GIT_STREAM_RW = (GIT_STREAM_RDONLY | GIT_STREAM_WRONLY),
} git_odb_stream_t;
-/** A stream to read/write from a backend */
+typedef struct git_hash_ctx git_hash_ctx;
+
+/**
+ * A stream to read/write from a backend.
+ *
+ * This represents a stream of data being written to or read from a
+ * backend. When writing, the frontend functions take care of
+ * calculating the object's id and all `finalize_write` needs to do is
+ * store the object with the id it is passed.
+ */
struct git_odb_stream {
git_odb_backend *backend;
unsigned int mode;
+ git_hash_ctx *hash_ctx;
+ /**
+ * Write at most `len` bytes into `buffer` and advance the
+ * stream.
+ */
int (*read)(git_odb_stream *stream, char *buffer, size_t len);
+
+ /**
+ * Write `len` bytes from `buffer` into the stream.
+ */
int (*write)(git_odb_stream *stream, const char *buffer, size_t len);
- int (*finalize_write)(git_oid *oid_p, git_odb_stream *stream);
+
+ /**
+ * Store the contents of the stream as an object with the id
+ * specified in `oid`.
+ */
+ int (*finalize_write)(git_odb_stream *stream, const git_oid *oid);
+
+ /**
+ * Free the stream's memory.
+ */
void (*free)(git_odb_stream *stream);
};
diff --git a/include/git2/sys/odb_backend.h b/include/git2/sys/odb_backend.h
index 3cd2734c0..31ffe1c33 100644
--- a/include/git2/sys/odb_backend.h
+++ b/include/git2/sys/odb_backend.h
@@ -48,12 +48,12 @@ struct git_odb_backend {
int (* read_header)(
size_t *, git_otype *, git_odb_backend *, const git_oid *);
- /* The writer may assume that the object
- * has already been hashed and is passed
- * in the first parameter.
+ /**
+ * Write an object into the backend. The id of the object has
+ * already been calculated and is passed in.
*/
int (* write)(
- git_oid *, git_odb_backend *, const void *, size_t, git_otype);
+ git_odb_backend *, const git_oid *, const void *, size_t, git_otype);
int (* writestream)(
git_odb_stream **, git_odb_backend *, size_t, git_otype);