Commit 483526ebec8b50682b6edd3e1db92df7569ca7b0

Vicent Marti 2011-03-28T22:23:44

Update the SQLite backend

diff --git a/src/backends/sqlite.c b/src/backends/sqlite.c
index 72d7b4d..a4c6d48 100644
--- a/src/backends/sqlite.c
+++ b/src/backends/sqlite.c
@@ -44,21 +44,20 @@ typedef struct {
 	sqlite3_stmt *st_read_header;
 } sqlite_backend;
 
-int sqlite_backend__read_header(git_rawobj *obj, git_odb_backend *_backend, const git_oid *oid)
+int sqlite_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backend *_backend, const git_oid *oid)
 {
 	sqlite_backend *backend;
 	int error;
 
-	assert(obj && _backend && oid);
+	assert(len_p && type_p && _backend && oid);
 
 	backend = (sqlite_backend *)_backend;
 	error = GIT_ERROR;
-	obj->data = NULL;
 
 	if (sqlite3_bind_text(backend->st_read_header, 1, (char *)oid->id, 20, SQLITE_TRANSIENT) == SQLITE_OK) {
 		if (sqlite3_step(backend->st_read_header) == SQLITE_ROW) {
-			obj->type = sqlite3_column_int(backend->st_read_header, 0);
-			obj->len = sqlite3_column_int(backend->st_read_header, 1);
+			*type_p = (git_otype)sqlite3_column_int(backend->st_read_header, 0);
+			*len_p = (size_t)sqlite3_column_int(backend->st_read_header, 1);
 			assert(sqlite3_step(backend->st_read_header) == SQLITE_DONE);
 			error = GIT_SUCCESS;
 		} else {
@@ -71,26 +70,26 @@ int sqlite_backend__read_header(git_rawobj *obj, git_odb_backend *_backend, cons
 }
 
 
-int sqlite_backend__read(git_rawobj *obj, git_odb_backend *_backend, const git_oid *oid)
+int sqlite_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend, const git_oid *oid)
 {
 	sqlite_backend *backend;
 	int error;
 
-	assert(obj && _backend && oid);
+	assert(data_p && len_p && type_p && _backend && oid);
 
 	backend = (sqlite_backend *)_backend;
 	error = GIT_ERROR;
 
 	if (sqlite3_bind_text(backend->st_read, 1, (char *)oid->id, 20, SQLITE_TRANSIENT) == SQLITE_OK) {
 		if (sqlite3_step(backend->st_read) == SQLITE_ROW) {
-			obj->type = sqlite3_column_int(backend->st_read, 0);
-			obj->len = sqlite3_column_int(backend->st_read, 1);
-			obj->data = git__malloc(obj->len);
+			*type_p = (git_otype)sqlite3_column_int(backend->st_read, 0);
+			*len_p = (size_t)sqlite3_column_int(backend->st_read, 1);
+			*data_p = git__malloc(*len_p);
 
-			if (obj->data == NULL) {
+			if (*data_p == NULL) {
 				error = GIT_ENOMEM;
 			} else {
-				memcpy(obj->data, sqlite3_column_blob(backend->st_read, 2), obj->len);
+				memcpy(*data_p, sqlite3_column_blob(backend->st_read, 2), *len_p);
 				error = GIT_SUCCESS;
 			}
 
@@ -126,27 +125,24 @@ int sqlite_backend__exists(git_odb_backend *_backend, const git_oid *oid)
 }
 
 
-int sqlite_backend__write(git_oid *id, git_odb_backend *_backend, git_rawobj *obj)
+int sqlite_backend__write(git_oid *id, git_odb_backend *_backend, const void *data, size_t len, git_otype type)
 {
-	char hdr[64];
-	int  hdrlen;
-
 	int error;
 	sqlite_backend *backend;
 
-	assert(id && _backend && obj);
+	assert(id && _backend && data);
 
 	backend = (sqlite_backend *)_backend;
 
-	if ((error = git_odb__hash_obj(id, hdr, sizeof(hdr), &hdrlen, obj)) < 0)
+	if ((error = git_odb_hash(id, data, len, type)) < 0)
 		return error;
 
 	error = SQLITE_ERROR;
 
 	if (sqlite3_bind_text(backend->st_write, 1, (char *)id->id, 20, SQLITE_TRANSIENT) == SQLITE_OK &&
-		sqlite3_bind_int(backend->st_write, 2, (int)obj->type) == SQLITE_OK &&
-		sqlite3_bind_int(backend->st_write, 3, obj->len) == SQLITE_OK &&
-		sqlite3_bind_blob(backend->st_write, 4, obj->data, obj->len, SQLITE_TRANSIENT) == SQLITE_OK) {
+		sqlite3_bind_int(backend->st_write, 2, (int)type) == SQLITE_OK &&
+		sqlite3_bind_int(backend->st_write, 3, len) == SQLITE_OK &&
+		sqlite3_bind_blob(backend->st_write, 4, data, len, SQLITE_TRANSIENT) == SQLITE_OK) {
 		error = sqlite3_step(backend->st_write);
 	}
 
diff --git a/tests/t11-sqlite.c b/tests/t11-sqlite.c
index fecf788..61ecf98 100644
--- a/tests/t11-sqlite.c
+++ b/tests/t11-sqlite.c
@@ -23,7 +23,7 @@
  * Boston, MA 02110-1301, USA.
  */
 #include "test_lib.h"
-
+#include "odb.h"
 
 #ifdef GIT2_SQLITE_BACKEND
 #include "t03-data.h"
@@ -31,14 +31,17 @@
 #include "git2/odb_backend.h"
 
 
-static int cmp_objects(raw_object *o1, raw_object *o2)
+static int cmp_objects(git_odb_object *odb_obj, git_rawobj *raw)
 {
-	if (o1->type != o2->type)
+	if (raw->type != git_odb_object_type(odb_obj))
 		return -1;
-	if (o1->len != o2->len)
+
+	if (raw->len != git_odb_object_size(odb_obj))
 		return -1;
-	if ((o1->len > 0) && (memcmp(o1->data, o2->data, o1->len) != 0))
+
+	if ((raw->len > 0) && (memcmp(raw->data, git_odb_object_data(odb_obj), raw->len) != 0))
 		return -1;
+
 	return 0;
 }
 
@@ -62,15 +65,15 @@ static git_odb *open_sqlite_odb(void)
 #define TEST_WRITE(PTR) {\
     git_odb *db; \
 	git_oid id1, id2; \
-    raw_object obj; \
+    git_odb_object *obj; \
 	db = open_sqlite_odb(); \
 	must_be_true(db != NULL); \
     must_pass(git_oid_mkstr(&id1, PTR.id)); \
-    must_pass(git_odb_write(&id2, db, &PTR##_obj)); \
+    must_pass(git_odb_write(&id2, db, PTR##_obj.data, PTR##_obj.len, PTR##_obj.type)); \
     must_be_true(git_oid_cmp(&id1, &id2) == 0); \
     must_pass(git_odb_read(&obj, db, &id1)); \
-    must_pass(cmp_objects(&obj, &PTR##_obj)); \
-    git_rawobj_close(&obj); \
+    must_pass(cmp_objects(obj, &PTR##_obj)); \
+    git_odb_object_close(obj); \
     git_odb_close(db); \
 }