odb_mempack.c 4.18 KB
Newer Older
Vicent Marti committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "common.h"
#include "git2/object.h"
#include "git2/sys/odb_backend.h"
#include "fileops.h"
#include "hash.h"
#include "odb.h"
#include "array.h"
#include "oidmap.h"

#include "git2/odb_backend.h"
#include "git2/types.h"
#include "git2/pack.h"

struct memobject {
	git_oid oid;
	size_t len;
	git_otype type;
25
	char data[GIT_FLEX_ARRAY];
Vicent Marti committed
26 27 28 29 30 31 32 33 34 35 36 37 38
};

struct memory_packer_db {
	git_odb_backend parent;
	git_oidmap *objects;
	git_array_t(struct memobject *) commits;
};

static int impl__write(git_odb_backend *_backend, const git_oid *oid, const void *data, size_t len, git_otype type)
{
	struct memory_packer_db *db = (struct memory_packer_db *)_backend;
	struct memobject *obj = NULL; 
	khiter_t pos;
39
	size_t alloc_len;
Vicent Marti committed
40 41
	int rval;

42
	pos = git_oidmap_put(db->objects, oid, &rval);
Vicent Marti committed
43 44 45 46 47 48
	if (rval < 0)
		return -1;

	if (rval == 0)
		return 0;

49 50
	GITERR_CHECK_ALLOC_ADD(&alloc_len, sizeof(struct memobject), len);
	obj = git__malloc(alloc_len);
Vicent Marti committed
51 52 53 54 55 56 57
	GITERR_CHECK_ALLOC(obj);

	memcpy(obj->data, data, len);
	git_oid_cpy(&obj->oid, oid);
	obj->len = len;
	obj->type = type;

58 59
	git_oidmap_set_key_at(db->objects, pos, &obj->oid);
	git_oidmap_set_value_at(db->objects, pos, obj);
Vicent Marti committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73

	if (type == GIT_OBJ_COMMIT) {
		struct memobject **store = git_array_alloc(db->commits);
		GITERR_CHECK_ALLOC(store);
		*store = obj;
	}

	return 0;
}

static int impl__exists(git_odb_backend *backend, const git_oid *oid)
{
	struct memory_packer_db *db = (struct memory_packer_db *)backend;

74
	return git_oidmap_exists(db->objects, oid);
Vicent Marti committed
75 76 77 78 79 80 81 82
}

static int impl__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid)
{
	struct memory_packer_db *db = (struct memory_packer_db *)backend;
	struct memobject *obj = NULL;
	khiter_t pos;

83
	pos = git_oidmap_lookup_index(db->objects, oid);
84
	if (!git_oidmap_valid_index(db->objects, pos))
Vicent Marti committed
85 86
		return GIT_ENOTFOUND;

87
	obj = git_oidmap_value_at(db->objects, pos);
Vicent Marti committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

	*len_p = obj->len;
	*type_p = obj->type;
	*buffer_p = git__malloc(obj->len);
	GITERR_CHECK_ALLOC(*buffer_p);

	memcpy(*buffer_p, obj->data, obj->len);
	return 0;
}

static int impl__read_header(size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid)
{
	struct memory_packer_db *db = (struct memory_packer_db *)backend;
	struct memobject *obj = NULL;
	khiter_t pos;

104
	pos = git_oidmap_lookup_index(db->objects, oid);
105
	if (!git_oidmap_valid_index(db->objects, pos))
Vicent Marti committed
106 107
		return GIT_ENOTFOUND;

108
	obj = git_oidmap_value_at(db->objects, pos);
Vicent Marti committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

	*len_p = obj->len;
	*type_p = obj->type;
	return 0;
}

int git_mempack_dump(git_buf *pack, git_repository *repo, git_odb_backend *_backend)
{
	struct memory_packer_db *db = (struct memory_packer_db *)_backend;
	git_packbuilder *packbuilder;
	uint32_t i;
	int err = -1;

	if (git_packbuilder_new(&packbuilder, repo) < 0)
		return -1;

	for (i = 0; i < db->commits.size; ++i) {
		struct memobject *commit = db->commits.ptr[i];

		err = git_packbuilder_insert_commit(packbuilder, &commit->oid);
		if (err < 0)
			goto cleanup;
	}

	err = git_packbuilder_write_buf(pack, packbuilder);

cleanup:
	git_packbuilder_free(packbuilder);
	return err;
}

void git_mempack_reset(git_odb_backend *_backend)
{
	struct memory_packer_db *db = (struct memory_packer_db *)_backend;
	struct memobject *object = NULL;

145
	git_oidmap_foreach_value(db->objects, object, {
Vicent Marti committed
146 147 148 149
		git__free(object);
	});

	git_array_clear(db->commits);
150

151
	git_oidmap_clear(db->objects);
Vicent Marti committed
152 153 154 155
}

static void impl__free(git_odb_backend *_backend)
{
156 157 158 159
	struct memory_packer_db *db = (struct memory_packer_db *)_backend;

	git_oidmap_free(db->objects);
	git__free(db);
Vicent Marti committed
160 161 162 163 164 165 166 167 168 169 170 171 172
}

int git_mempack_new(git_odb_backend **out)
{
	struct memory_packer_db *db;

	assert(out);

	db = git__calloc(1, sizeof(struct memory_packer_db));
	GITERR_CHECK_ALLOC(db);

	db->objects = git_oidmap_alloc();

173
	db->parent.version = GIT_ODB_BACKEND_VERSION;
Vicent Marti committed
174 175 176 177 178 179 180 181 182
	db->parent.read = &impl__read;
	db->parent.write = &impl__write;
	db->parent.read_header = &impl__read_header;
	db->parent.exists = &impl__exists;
	db->parent.free = &impl__free;

	*out = (git_odb_backend *)db;
	return 0;
}