[darwinbuild-changes] [714] branches/PR-7489777/darwinup

source_changes at macosforge.org source_changes at macosforge.org
Tue Feb 23 13:46:38 PST 2010


Revision: 714
          http://trac.macosforge.org/projects/darwinbuild/changeset/714
Author:   wsiegrist at apple.com
Date:     2010-02-23 13:46:35 -0800 (Tue, 23 Feb 2010)
Log Message:
-----------
Implement get_files/get_all_ordered

Modified Paths:
--------------
    branches/PR-7489777/darwinup/DB.cpp
    branches/PR-7489777/darwinup/DB.h
    branches/PR-7489777/darwinup/Database.cpp
    branches/PR-7489777/darwinup/Database.h
    branches/PR-7489777/darwinup/Depot.cpp
    branches/PR-7489777/darwinup/Table.cpp

Modified: branches/PR-7489777/darwinup/DB.cpp
===================================================================
--- branches/PR-7489777/darwinup/DB.cpp	2010-02-23 17:44:23 UTC (rev 713)
+++ branches/PR-7489777/darwinup/DB.cpp	2010-02-23 21:46:35 UTC (rev 714)
@@ -353,17 +353,32 @@
 							   1,
 							   this->m_archives_table->column(4), // active
 							   '=', (uint64_t)0);
-	if (res == SQLITE_DONE && *count) return (DB_OK & DB_FOUND);
+	if (res == SQLITE_DONE && *count) return (DB_OK | DB_FOUND);
 	if (res == SQLITE_DONE) return DB_OK;
 	return DB_ERROR;
 }
 
+int DarwinupDatabase::get_files(uint8_t*** data, uint32_t* count, Archive* archive) {
+	int res = this->get_all_ordered("files__archive",
+									data, count,
+									this->m_files_table,
+									this->m_files_table->column(8), // order by path
+									ORDER_BY_ASC,
+									1,
+									this->m_files_table->column(1),
+									'=', archive->serial());
+	
+	if ((res == SQLITE_DONE) && *count) return (DB_OK | DB_FOUND);
+	if (res == SQLITE_DONE) return DB_OK;
+	return DB_ERROR;
+}
+
 int DarwinupDatabase::get_file_serials(uint64_t** serials, uint32_t* count) {
 	int res = this->get_column("file_serials", (void**)serials, count, 
 							   this->m_files_table,
 							   this->m_files_table->column(0),
 							   0);
-	if (res == SQLITE_DONE && *count) return (DB_OK & DB_FOUND);
+	if (res == SQLITE_DONE && *count) return (DB_OK | DB_FOUND);
 	if (res == SQLITE_DONE) return DB_OK;
 	return DB_ERROR;	
 }

Modified: branches/PR-7489777/darwinup/DB.h
===================================================================
--- branches/PR-7489777/darwinup/DB.h	2010-02-23 17:44:23 UTC (rev 713)
+++ branches/PR-7489777/darwinup/DB.h	2010-02-23 21:46:35 UTC (rev 714)
@@ -43,14 +43,7 @@
 #include "Digest.h"
 #include "File.h"
 
-// return code bits
-#define DB_OK        0x0000
-#define DB_ERROR     0x0001
-#define DB_FOUND     0x0010
 
-#define FOUND(x)  ((x & DB_FOUND) && !(x & DB_ERROR))
-
-
 /**
  *
  * Darwinup database abstraction. This class is responsible
@@ -90,6 +83,7 @@
 	int      get_next_file(uint8_t** data, File* file, file_starseded_t star);
 	int      get_file_serials(uint64_t** serials, uint32_t* count);
 	int      get_file_serial_from_archive(Archive* archive, const char* path, uint64_t** serial);
+	int      get_files(uint8_t*** data, uint32_t* count, Archive* archive);
 	int      file_offset(int column);
 	int      update_file(uint64_t serial, Archive* archive, uint32_t info, mode_t mode, 
 						 uid_t uid, gid_t gid, Digest* digest, const char* path);

Modified: branches/PR-7489777/darwinup/Database.cpp
===================================================================
--- branches/PR-7489777/darwinup/Database.cpp	2010-02-23 17:44:23 UTC (rev 713)
+++ branches/PR-7489777/darwinup/Database.cpp	2010-02-23 21:46:35 UTC (rev 714)
@@ -301,7 +301,6 @@
 #define __bind_va_columns(_lastarg) \
     va_list args; \
     va_start(args, _lastarg); \
-    fprintf(stderr, "DEBUG: sql: %s \n", sqlite3_sql(stmt)); \
     for (uint32_t i=0; i<count; i++) { \
         Column* col = va_arg(args, Column*); \
         va_arg(args, int); \
@@ -428,7 +427,7 @@
 	return res;
 }
 
-int Database::step_column(sqlite3_stmt* stmt, void** output, uint32_t size, uint32_t* count) {
+int Database::step_all(sqlite3_stmt* stmt, void** output, uint32_t size, uint32_t* count) {
 	uint32_t used = 0;
 	uint32_t total_used = used;
 	uint32_t rowsize = size / INITIAL_ROWS;
@@ -448,7 +447,7 @@
 			*output = realloc(*output, size);
 			IF_DEBUG("reallocating: output = %p  size = %u \n", *output, size);
 			if (!*output) {
-				fprintf(stderr, "Error: ran out of memory in Database::step_once \n");
+				fprintf(stderr, "Error: ran out of memory in Database::step_all \n");
 				return SQLITE_ERROR;
 			}
 		}		
@@ -482,7 +481,7 @@
 	uint32_t size = INITIAL_ROWS * column->size();
 	*output = malloc(size);
 	IF_DEBUG("get_column output = %p  size = %u \n", *output, size);
-	res = this->step_column(stmt, output, size, result_count);
+	res = this->step_all(stmt, output, size, result_count);
 	IF_DEBUG("get_colu output(%p) = %llu \n", *output, **(uint64_t**)output);
 	sqlite3_reset(stmt);
 	cache_release_value(m_statement_cache, &stmt);
@@ -520,7 +519,50 @@
 	return res;
 }
 
+int Database::get_all_ordered(const char* name, uint8_t*** output, uint32_t* result_count,
+							  Table* table, Column* order_by, int order, uint32_t count, ...) {
+	__get_stmt(table->get_row_ordered(m_db, order_by, order, count, args));
+	IF_DEBUG("stmt = %s \n", sqlite3_sql(stmt));
+	int res = SQLITE_OK;
+	uint32_t param = 1;
+	__bind_va_columns(count);
+	uint8_t* current = NULL;
+	*result_count = 0;
+	uint32_t output_max = INITIAL_ROWS;
+	*output = (uint8_t**)calloc(output_max, sizeof(uint8_t*));
+	
+	res = SQLITE_ROW;
+	while (res == SQLITE_ROW) {
+		if ((*result_count) >= output_max) {
+			output_max *= REALLOC_FACTOR;
+			*output = (uint8_t**)realloc((*output), output_max * sizeof(uint8_t*));
+			if (!(*output)) {
+				fprintf(stderr, "Error: ran out of memory trying to realloc output"
+						        "in get_all_ordered.\n");
+				return DB_ERROR;
+			}
+			IF_DEBUG("get_all_ordered realloc: %p \n", *output);
+		}
+		current = table->alloc_result();
+		IF_DEBUG("Table::alloc_result = %p \n", current);
+		res = this->step_once(stmt, current, NULL);
+		if (res == SQLITE_ROW) {
+			(*output)[(*result_count)] = current;
+			IF_DEBUG("get_all_ordered count: %u output(%p) = %llu \n", 
+					 (*result_count), (*output)[(*result_count)], (uint64_t)(*output)[(*result_count)][0]);
+			(*result_count)++;
+		} else {
+			table->free_result(current);
+		}
+	}
 
+	sqlite3_reset(stmt);
+	cache_release_value(m_statement_cache, &stmt);
+	IF_DEBUG("get_all_ordered res = %d \n", res);
+	return res;
+}
+
+
 int Database::sql(const char* name, const char* fmt, ...) {
 	sqlite3_stmt* stmt;
 	char* key = strdup(name);

Modified: branches/PR-7489777/darwinup/Database.h
===================================================================
--- branches/PR-7489777/darwinup/Database.h	2010-02-23 17:44:23 UTC (rev 713)
+++ branches/PR-7489777/darwinup/Database.h	2010-02-23 21:46:35 UTC (rev 714)
@@ -57,9 +57,16 @@
 #define REALLOC_FACTOR 4
 #define ERROR_BUF_SIZE 1024
 
+// return code bits
+#define DB_OK        0x0000
+#define DB_ERROR     0x0001
+#define DB_FOUND     0x0010
 
+#define FOUND(x)  ((x & DB_FOUND) && !(x & DB_ERROR))
 
 
+
+
 // libcache callbacks
 bool cache_key_is_equal(void* key1, void* key2, void* user);
 void cache_key_retain(void* key_in, void** key_out, void* user_data);
@@ -111,6 +118,8 @@
 	int  get_row(const char* name, uint8_t** output, Table* table, uint32_t count, ...);
 	int  get_row_ordered(const char* name, uint8_t** output, Table* table, Column* order_by, 
 						 int order, uint32_t count, ...);
+	int  get_all_ordered(const char* name, uint8_t*** output, uint32_t* result_count,
+						 Table* table, Column* order_by, int order, uint32_t count, ...);
 	int  update_value(const char* name, Table* table, Column* value_column, void** value, 
 					  uint32_t count, ...);
 	int  update(Table* table, uint64_t pkvalue, ...);
@@ -135,7 +144,7 @@
 	
 	size_t store_column(sqlite3_stmt* stmt, int column, uint8_t* output);
 	int step_once(sqlite3_stmt* stmt, uint8_t* output, uint32_t* used);
-	int step_column(sqlite3_stmt* stmt, void** output, uint32_t size, uint32_t* count);
+	int step_all(sqlite3_stmt* stmt, void** output, uint32_t size, uint32_t* count);
 	
 	// libcache
 	void init_cache();

Modified: branches/PR-7489777/darwinup/Depot.cpp
===================================================================
--- branches/PR-7489777/darwinup/Depot.cpp	2010-02-23 17:44:23 UTC (rev 713)
+++ branches/PR-7489777/darwinup/Depot.cpp	2010-02-23 21:46:35 UTC (rev 714)
@@ -250,7 +250,8 @@
 			query = "SELECT serial, uuid, name, info, date_added FROM archives ORDER BY serial DESC";
 		}
 		res = sqlite3_prepare(m_db, query, -1, &stmt, NULL);
-		if (res != 0) fprintf(stderr, "%s:%d: sqlite3_prepare: %s: %s (%d)\n", __FILE__, __LINE__, query, sqlite3_errmsg(m_db), res);
+		if (res != 0) fprintf(stderr, "%s:%d: sqlite3_prepare: %s: %s (%d)\n", 
+							  __FILE__, __LINE__, query, sqlite3_errmsg(m_db), res);
 	}
 	if (stmt && res == 0) {
 		size_t i = 0;
@@ -285,53 +286,28 @@
 }
 
 int Depot::iterate_files(Archive* archive, FileIteratorFunc func, void* context) {
-	int res = 0;
-	static sqlite3_stmt* stmt = NULL;
-	if (stmt == NULL && m_db) {
-		const char* query = "SELECT serial, info, path, mode, uid, gid, size, digest FROM files WHERE archive=? ORDER BY path";
-		res = sqlite3_prepare(m_db, query, -1, &stmt, NULL);
-		if (res != 0) fprintf(stderr, "%s:%d: sqlite3_prepare: %s: %s (%d)\n", __FILE__, __LINE__, query, sqlite3_errmsg(m_db), res);
-	}
-	if (stmt && res == 0) {
-		res = sqlite3_bind_int64(stmt, 1, archive->serial());
-		while (res == 0) {
-			res = sqlite3_step(stmt);
-			if (res == SQLITE_ROW) {
-				res = 0;
-				int i = 0;
-				uint64_t serial = sqlite3_column_int64(stmt, i++);
-				uint32_t info = sqlite3_column_int(stmt, i++);
-				const unsigned char* path = sqlite3_column_text(stmt, i++);
-				mode_t mode = sqlite3_column_int(stmt, i++);
-				uid_t uid = sqlite3_column_int(stmt, i++);
-				gid_t gid = sqlite3_column_int(stmt, i++);
-				off_t size = sqlite3_column_int64(stmt, i++);
-				const void* blob = sqlite3_column_blob(stmt, i);
-				int blobsize = sqlite3_column_bytes(stmt, i++);
-
-				Digest* digest = NULL;
-				if (blobsize > 0) {
-					digest = new Digest();
-					digest->m_size = blobsize;
-					memcpy(digest->m_data, blob, ((size_t)blobsize < sizeof(digest->m_data)) ? blobsize : sizeof(digest->m_data));
-				}
-
-				File* file = FileFactory(serial, archive, info, (const char*)path, mode, uid, gid, size, digest);
-				if (file) {
-					res = func(file, context);
-					delete file;
-				} else {
-					fprintf(stderr, "%s:%d: FileFactory returned NULL\n", __FILE__, __LINE__);
-					res = -1;
-					break;
-				}
-			} else if (res == SQLITE_DONE) {
-				res = 0;
+	int res = DB_OK;
+	uint8_t** filelist;
+	uint32_t count;
+	res = this->m_db2->get_files(&filelist, &count, archive);
+	IF_DEBUG("iterate_files for count %d from get_files, res: %d \n", count, res);
+	if (FOUND(res)) {
+		for (uint32_t i=0; i < count; i++) {
+			File* file = this->m_db2->make_file(filelist[i]);
+			IF_DEBUG("make_file gave back file %llu \n", file->serial());
+			if (file) {
+				res = func(file, context);
+				delete file;
+			} else {
+				fprintf(stderr, "%s:%d: DB::make_file returned NULL\n", __FILE__, __LINE__);
+				res = -1;
 				break;
 			}
 		}
-		sqlite3_reset(stmt);
+	} else {
+		IF_DEBUG("iterate_files for archive (%llu) found no files \n", archive->serial());
 	}
+
 	return res;
 }
 

Modified: branches/PR-7489777/darwinup/Table.cpp
===================================================================
--- branches/PR-7489777/darwinup/Table.cpp	2010-02-23 17:44:23 UTC (rev 713)
+++ branches/PR-7489777/darwinup/Table.cpp	2010-02-23 21:46:35 UTC (rev 714)
@@ -280,9 +280,7 @@
     char not_op = ' '; \
 	int len; \
 	for (uint32_t i=0; i < count; i++) { \
-        fprintf(stderr, "DEBUG: __where_va i=%u \n", i); \
 		Column* col = va_arg(args, Column*); \
-		fprintf(stderr, "DEBUG: __where_va col %p \n", col); \
         tmp_op = va_arg(args, int); \
         if (tmp_op == '!') { \
             not_op = tmp_op; \
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100223/4ba4e5b2/attachment-0001.html>


More information about the darwinbuild-changes mailing list