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

source_changes at macosforge.org source_changes at macosforge.org
Mon Feb 8 11:05:10 PST 2010


Revision: 697
          http://trac.macosforge.org/projects/darwinbuild/changeset/697
Author:   wsiegrist at apple.com
Date:     2010-02-08 11:05:07 -0800 (Mon, 08 Feb 2010)
Log Message:
-----------
Implemented delete apis, added more debugging output

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/Depot.h
    branches/PR-7489777/darwinup/Table.cpp
    branches/PR-7489777/darwinup/Table.h
    branches/PR-7489777/darwinup/main.cpp

Modified: branches/PR-7489777/darwinup/DB.cpp
===================================================================
--- branches/PR-7489777/darwinup/DB.cpp	2010-02-05 21:56:15 UTC (rev 696)
+++ branches/PR-7489777/darwinup/DB.cpp	2010-02-08 19:05:07 UTC (rev 697)
@@ -96,9 +96,9 @@
 								   uid_t uid, gid_t gid, Digest* digest) {
 
 	bool res = false;
-	
 	// get the serial for the file where archive and path match
 	uint64_t serial;
+	
 	res = this->get_value("file_serial__archive_path",
 						  (void**)&serial,
 						  this->m_files_table,
@@ -108,7 +108,7 @@
 						  (uint64_t)archive->serial(),
 						  this->m_files_table->column(8), // path
 						  path);
-									  
+								  
 	// update the information
 	res = this->update(this->m_files_table, serial,
 					   (uint64_t)archive->serial(),
@@ -120,6 +120,7 @@
 					   (uint8_t*)(digest ? digest->data() : NULL), 
 					   (uint32_t)(digest ? digest->size() : 0), 
 					   path);
+
 	if (!res) {
 		fprintf(stderr, "Error: unable to update file with serial %llu and path %s: %s \n",
 				serial, path, this->error());
@@ -153,16 +154,41 @@
 uint64_t DarwinupDatabase::count_files(Archive* archive, const char* path) {
 
 	bool res = false;
-	uint64_t c = 1234;
+	uint64_t* c = (uint64_t*)malloc(sizeof(uint64_t));
 	res = this->count("count_files",
-					  (void**)&c,
+					  (void**)c,
 					  this->m_files_table,
-					  2,                              // number of where conditions
+					  2, // number of where conditions
 					  this->m_files_table->column(1), // archive
 					  (uint64_t)archive->serial(),
 					  this->m_files_table->column(8), // path
 					  path);
-	fprintf(stderr, "COUNT: Database::count() gave us %llu\n", c);
-	return c;
+	return *c;
 }
 
+
+bool DarwinupDatabase::delete_archive(Archive* archive) {
+	return this->del(this->m_archives_table, archive->serial());
+}
+
+bool DarwinupDatabase::delete_archive(uint64_t serial) {
+	return this->del(this->m_archives_table, serial);
+}
+
+bool DarwinupDatabase::delete_file(File* file) {
+	return this->del(this->m_files_table, file->serial());
+}
+
+bool DarwinupDatabase::delete_file(uint64_t serial) {
+	return this->del(this->m_files_table, serial);
+}
+
+bool DarwinupDatabase::delete_files(Archive* archive) {
+	return this->del("delete_files__archive",
+					 this->m_files_table,
+					 1,
+					 this->m_files_table->column(1),  // archive
+					 (uint64_t)archive->serial());
+}
+
+

Modified: branches/PR-7489777/darwinup/DB.h
===================================================================
--- branches/PR-7489777/darwinup/DB.h	2010-02-05 21:56:15 UTC (rev 696)
+++ branches/PR-7489777/darwinup/DB.h	2010-02-08 19:05:07 UTC (rev 697)
@@ -41,6 +41,7 @@
 #include "Table.h"
 #include "Archive.h"
 #include "Digest.h"
+#include "File.h"
 
 
 /**
@@ -59,13 +60,21 @@
 	
 	uint64_t count_files(Archive* archive, const char* path);
 	
-	// inserts into tables, returns serial from primary key
+	// Archives table modifications
 	uint64_t insert_archive(uuid_t uuid, uint32_t info, const char* name, time_t date);
-	bool update_file(Archive* archive, const char* path, uint32_t info, mode_t mode, 
-					 uid_t uid, gid_t gid, Digest* digest);
+	bool     delete_archive(Archive* archive);
+	bool     delete_archive(uint64_t serial);
+
+	// Files table modifications
+	bool     update_file(Archive* archive, const char* path, uint32_t info, mode_t mode, 
+						 uid_t uid, gid_t gid, Digest* digest);
 	uint64_t insert_file(uint32_t info, mode_t mode, uid_t uid, gid_t gid, 
 						 Digest* digest, Archive* archive, const char* path);
+	bool     delete_file(uint64_t serial);
+	bool     delete_file(File* file);
+	bool     delete_files(Archive* archive);
 	
+
 protected:
 	
 	Table*        m_archives_table;

Modified: branches/PR-7489777/darwinup/Database.cpp
===================================================================
--- branches/PR-7489777/darwinup/Database.cpp	2010-02-05 21:56:15 UTC (rev 696)
+++ branches/PR-7489777/darwinup/Database.cpp	2010-02-08 19:05:07 UTC (rev 697)
@@ -82,10 +82,11 @@
 
 
 void Database::init_cache() {
+	fprintf(stderr, "CACHE: init_cache \n");
 	cache_attributes_t attrs;
 	attrs.version = CACHE_ATTRIBUTES_VERSION_2;
 	attrs.key_hash_cb = cache_key_hash_cb_cstring;
-	attrs.key_is_equal_cb = cache_key_is_equal_cb_cstring;
+	attrs.key_is_equal_cb = cache_key_is_equal;
 	attrs.key_retain_cb = cache_key_retain;
 	attrs.key_release_cb = cache_key_release;
 	attrs.value_release_cb = cache_value_release;
@@ -94,9 +95,17 @@
 }
 
 void Database::destroy_cache() {
+	fprintf(stderr, "CACHE: destroy_cache \n");
 	cache_destroy(m_statement_cache);
 }
 
+bool cache_key_is_equal(void* key1, void* key2, void* user) {
+	fprintf(stderr, "CACHE: key1: %s key2: %s \n", (char*)key1, (char*)key2);
+	bool res = (strcmp((char*)key1, (char*)key2) == 0);
+	fprintf(stderr, "CACHE: key_is_equal returning %d \n", res);
+	return res;
+}
+
 void cache_key_retain(void* key_in, void** key_out, void* user_data) {
 	fprintf(stderr, "CACHE: key_retain %s\n", (char*)key_in);
 	*key_out = strdup((char*)key_in);
@@ -298,6 +307,7 @@
 #define __get_stmt(expr) \
 	sqlite3_stmt* stmt; \
 	char* key = strdup(name); \
+    fprintf(stderr, "CACHE: statement cache at %p \n", m_statement_cache); \
 	cache_get_and_retain(m_statement_cache, key, (void**)&stmt); \
 	if (!stmt) { \
 		fprintf(stderr, "DEBUG: generating query for %s \n", key); \
@@ -312,36 +322,41 @@
 	} \
 	free(key);
 
-#define __step_and_store(stmt, type, output) \
-    res = sqlite3_step(stmt); \
+#define __step_and_store(_stmt, _type, _output) \
+    fprintf(stderr, "DEBUG: _stmt = %p \n", _stmt); \
+    fprintf(stderr, "DEBUG: _output = %p \n", _output); \
+    fprintf(stderr, "DEBUG: query = -%s- \n", sqlite3_sql(_stmt)); \
+    fprintf(stderr, "DEBUG: col count: %d \n", sqlite3_column_count(_stmt)); \
+    res = sqlite3_step(_stmt); \
     if (res == SQLITE_ROW) { \
-	    switch(type) { \
+	    switch(_type) { \
 		    case TYPE_INTEGER: \
 				fprintf(stderr, "DEBUG: step and store : integer\n"); \
-			    *(uint64_t*)output = sqlite3_column_int64(stmt, 0); \
-				fprintf(stderr, "DEBUG: step and store : %llu\n", *(uint64_t*)output); \
+			    *(uint64_t*)_output = (uint64_t)sqlite3_column_int64(_stmt, 0); \
+				fprintf(stderr, "DEBUG: step and store : %p %llu\n", (uint64_t*)_output, *(uint64_t*)_output); \
 			    break; \
 		    case TYPE_TEXT: \
 				fprintf(stderr, "DEBUG: step and store : text\n"); \
-			    *(const unsigned char**)output = sqlite3_column_text(stmt, 0); \
-				fprintf(stderr, "DEBUG: step and store : %s\n", *(char**)output); \
+			    *(const unsigned char**)_output = sqlite3_column_text(_stmt, 0); \
+				fprintf(stderr, "DEBUG: step and store : %s\n", *(char**)_output); \
 			    break; \
     		case TYPE_BLOB: \
 				fprintf(stderr, "DEBUG: step and store : blob\n"); \
-	    		*(const void**)output = sqlite3_column_blob(stmt, 0); \
+	    		*(const void**)_output = sqlite3_column_blob(_stmt, 0); \
 		    	break; \
     	} \
+    } else { \
+        fprintf(stderr, "ERROR: %d \n", res); \
     } \
-    sqlite3_reset(stmt); \
-	cache_release_value(m_statement_cache, &stmt);
+    sqlite3_reset(_stmt); \
+    cache_release_value(m_statement_cache, &_stmt);
 
 bool Database::count(const char* name, void** output, Table* table, uint32_t count, ...) {
-	__get_stmt(table->count(m_db, count, args))
+	__get_stmt(table->count(m_db, count, args));
 	int res = SQLITE_OK;
 	uint32_t param = 1;
 	__bind_va_columns(count);
-	int type = TYPE_INTEGER;
-	__step_and_store(stmt, type, output)
+	__step_and_store(stmt, TYPE_INTEGER, output);
 	return output != NULL;
 }
 
@@ -351,7 +366,7 @@
 	int res = SQLITE_OK;
 	uint32_t param = 1;
 	__bind_va_columns(count);
-	__step_and_store(stmt, value_column->type(), output)
+	__step_and_store(stmt, value_column->type(), output);
 	return output != NULL;
 }
 
@@ -411,8 +426,33 @@
 	return res == SQLITE_OK;
 }
 
-#undef __bind_params
 
+bool Database::del(Table* table, uint64_t serial) {
+	int res = SQLITE_OK;
+	sqlite3_stmt* stmt = table->del(m_db);
+	if (!stmt) {
+		fprintf(stderr, "Error: %s table gave a NULL statement when trying to delete.\n", table->name());
+		return false;
+	}
+	res = sqlite3_bind_int64(stmt, 1, serial);
+	res = this->execute(stmt);
+	return res == SQLITE_OK;
+}
+
+bool Database::del(const char* name, Table* table, uint32_t count, ...) {
+	__get_stmt(table->del(m_db, count, args));
+	int res = SQLITE_OK;
+	uint32_t param = 1;
+	__bind_va_columns(count);
+	res = this->execute(stmt);
+	return res == SQLITE_OK;
+	
+}
+
+#undef __bind_all_columns
+#undef __get_stmt
+#undef __step_and_store
+
 uint64_t Database::last_insert_id() {
 	return (uint64_t)sqlite3_last_insert_rowid(m_db);
 }

Modified: branches/PR-7489777/darwinup/Database.h
===================================================================
--- branches/PR-7489777/darwinup/Database.h	2010-02-05 21:56:15 UTC (rev 696)
+++ branches/PR-7489777/darwinup/Database.h	2010-02-08 19:05:07 UTC (rev 697)
@@ -48,6 +48,7 @@
 #include "Archive.h"
 
 // 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);
 void cache_key_release(void* key, void* user_data);
 void cache_value_retain(void* value, void* user_data);
@@ -84,7 +85,6 @@
 	const char* get_column(Table* table, Column* column, const char* where);
 	const char* get_all(Table* table, const char* where);
 		
-	bool del(Table* table, const char* where, uint32_t &count);
 	
 	
 	/**
@@ -104,6 +104,9 @@
 	bool update(Table* table, uint64_t pkvalue, ...);
 	bool insert(Table* table, ...);
 
+	bool del(Table* table, uint64_t serial);
+	bool del(const char* name, Table* table, uint32_t count, ...);
+	
 	bool add_table(Table*);
 	uint64_t last_insert_id();
 	

Modified: branches/PR-7489777/darwinup/Depot.cpp
===================================================================
--- branches/PR-7489777/darwinup/Depot.cpp	2010-02-05 21:56:15 UTC (rev 696)
+++ branches/PR-7489777/darwinup/Depot.cpp	2010-02-08 19:05:07 UTC (rev 697)
@@ -143,6 +143,11 @@
 	return (m_db != NULL);
 }
 
+
+DarwinupDatabase* Depot::get_db2() {
+	return m_db2;
+}
+
 // Unserialize an archive from the database.
 // Find the archive by UUID.
 // XXX: should be memoized
@@ -931,7 +936,7 @@
 	for (i = 0; i < context.files_to_remove->count; ++i) {
 		uint64_t serial = context.files_to_remove->values[i];
 		IF_DEBUG("deleting file %lld\n", serial);
-		if (res == 0) res = SQL("DELETE FROM files WHERE serial=%lld;", serial);
+		if (res == 0) res = m_db2->delete_file(serial);
 	}
 	if (res == 0) res = this->commit_transaction();
 
@@ -1255,7 +1260,7 @@
 	}
 
 	free(path);
-	return res;
+	return res != true;
 }
 
 int Depot::has_file(Archive* archive, File* file) {
@@ -1270,25 +1275,27 @@
 	
 	uint64_t count = m_db2->count_files(archive, relpath);
 	
-	fprintf(stderr, "COUNT=%llu\n", count);
-	
 	free(path);
 	return count > 0;
 }
 
 int Depot::remove(Archive* archive) {
-	int res = 0;
-	uint64_t serial = archive->serial();
-	if (res == 0) res = SQL("DELETE FROM files WHERE archive=%lld", serial);
-	if (res == 0) res = SQL("DELETE FROM archives WHERE serial=%lld", serial);
-	return res;
+	bool res = true;
+	res = m_db2->delete_files(archive);
+	if (!res) {
+		fprintf(stderr, "Error: unable to delete files for archive %llu \n", archive->serial());
+		return false;
+	}
+	res = m_db2->delete_archive(archive);
+	if (!res) {
+		fprintf(stderr, "Error: unable to delete archive %llu \n", archive->serial());
+		return false;
+	}
+	return res == true;
 }
 
 int Depot::remove(File* file) {
-	int res = 0;
-	uint64_t serial = file->serial();
-	if (res == 0) res = SQL("DELETE FROM files WHERE serial=%lld", serial);
-	return res;
+	return m_db2->delete_file(file) != true;
 }
 
 // helper to dispatch the actual command for process_archive()

Modified: branches/PR-7489777/darwinup/Depot.h
===================================================================
--- branches/PR-7489777/darwinup/Depot.h	2010-02-05 21:56:15 UTC (rev 696)
+++ branches/PR-7489777/darwinup/Depot.h	2010-02-08 19:05:07 UTC (rev 697)
@@ -111,6 +111,9 @@
 	int is_locked();
 
 	int has_file(Archive* archive, File* file);
+
+	// XXX: remove me
+	DarwinupDatabase* get_db2();
 	
 	protected:
 

Modified: branches/PR-7489777/darwinup/Table.cpp
===================================================================
--- branches/PR-7489777/darwinup/Table.cpp	2010-02-05 21:56:15 UTC (rev 696)
+++ branches/PR-7489777/darwinup/Table.cpp	2010-02-08 19:05:07 UTC (rev 697)
@@ -36,6 +36,17 @@
 #include "Table.h"
 
 
+// XXX
+void __hex_str(const char* s) {
+	int len = strlen(s);
+	fprintf(stderr, "HEXSTR: %d \n", len);
+	for (int i=0; i <= len; i++) {
+		fprintf(stderr, "%02x", (unsigned int)s[i]);
+	}
+	fprintf(stderr, "\n");
+}
+
+
 Table::Table() {
 	m_column_max    = 1;
 	m_column_count  = 0;
@@ -63,9 +74,12 @@
 	free(m_create_sql);
 	free(m_insert_sql);
 	free(m_update_sql);
+	free(m_delete_sql);
 	
 	sqlite3_finalize(m_prepared_insert);
 	sqlite3_finalize(m_prepared_update);
+	sqlite3_finalize(m_prepared_delete);
+	
 }
 
 
@@ -199,13 +213,9 @@
 	strlcpy(query, "SELECT count(*) FROM ", size);
 	__check_and_cat(m_name);
 	__check_and_cat(" WHERE 1");
-	
 	__where_va_columns;
-	
 	strlcat(query, ";", size);
-	
-	IF_DEBUG("[TABLE] count query: %s \n", query);
-
+	IF_DEBUG("[TABLE] count query: %s \n", query);	
 	__prepare_stmt;
 
 	return stmt;	
@@ -218,13 +228,9 @@
 	__check_and_cat(" FROM ");
 	__check_and_cat(m_name);
 	__check_and_cat(" WHERE 1");
-
 	__where_va_columns;
-	
 	strlcat(query, ";", size);
-
 	IF_DEBUG("[TABLE] get_value query: %s \n", query);
-
 	__prepare_stmt;
 	
 	return stmt;
@@ -350,3 +356,58 @@
 	return this->m_column_count;
 }
 
+
+sqlite3_stmt* Table::del(sqlite3* db) {
+	// we only need to prepare once, return if we already have it
+	if (m_prepared_delete) return m_prepared_delete;
+
+	uint32_t i = 0;
+	
+	// generate the sql query
+	size_t size = 22 + strlen(m_name);
+	for (i=0; i<m_column_count; i++) {
+		if (m_columns[i]->is_pk()) {
+			size += strlen(m_columns[i]->name()) + 2;
+			break;
+		}
+	}
+	m_delete_sql = (char*)malloc(size);
+	strlcpy(m_delete_sql, "DELETE FROM ", size);
+	strlcat(m_delete_sql, m_name, size);
+	
+	// WHERE statement using primary keys
+	strlcat(m_delete_sql, " WHERE ", size);
+	for (i=0; i<m_column_count; i++) {
+		if (m_columns[i]->is_pk()) {
+			strlcat(m_delete_sql, m_columns[i]->name(), size);
+			strlcat(m_delete_sql, "=?", size);
+			break;
+		}
+	}
+	strlcat(m_delete_sql, ";", size);
+	
+	IF_DEBUG("[TABLE] prepared delete: %s \n", m_delete_sql);
+	
+	// prepare
+	int res = sqlite3_prepare_v2(db, m_delete_sql, strlen(m_delete_sql), &m_prepared_delete, NULL);
+	if (res != SQLITE_OK) {
+		fprintf(stderr, "Error: unable to prepare delete statement for table: %s \n", m_name);
+		return NULL;
+	}
+	return m_prepared_delete;
+	
+}
+
+sqlite3_stmt* Table::del(sqlite3* db, uint32_t count, va_list args) {
+	__alloc_stmt_query;
+	strlcpy(query, "DELETE FROM ", size);
+	__check_and_cat(m_name);
+	__check_and_cat(" WHERE 1");
+	__where_va_columns;
+	strlcat(query, ";", size);
+	IF_DEBUG("[TABLE] delete query: %s \n", query);	
+	__prepare_stmt;
+	
+	return stmt;
+}
+

Modified: branches/PR-7489777/darwinup/Table.h
===================================================================
--- branches/PR-7489777/darwinup/Table.h	2010-02-05 21:56:15 UTC (rev 696)
+++ branches/PR-7489777/darwinup/Table.h	2010-02-08 19:05:07 UTC (rev 697)
@@ -54,15 +54,16 @@
 	char*    drop();    
 	char*    select(const char* where);
 	char*    select_column(const char* column, const char* where);		
-	char*    del(const char* where, uint32_t &count);
 
+
 	sqlite3_stmt*    count(sqlite3* db);
 	sqlite3_stmt*    count(sqlite3* db, uint32_t count, va_list args);
 	sqlite3_stmt*    get_value(sqlite3* db, Column* value_column, uint32_t count, va_list args);
 	sqlite3_stmt*    insert(sqlite3* db);
 	sqlite3_stmt*    update(sqlite3* db);
+	sqlite3_stmt*    del(sqlite3* db);
+	sqlite3_stmt*    del(sqlite3* db, uint32_t count, va_list args);
 	
-	
 protected:
 		
 	char*          m_name;
@@ -70,6 +71,7 @@
 	char*          m_create_sql;
 	char*          m_insert_sql;
 	char*          m_update_sql;
+	char*          m_delete_sql;
 	
 	Column**       m_columns;
 	uint32_t       m_column_count;
@@ -77,6 +79,8 @@
 	
 	sqlite3_stmt*  m_prepared_insert;
 	sqlite3_stmt*  m_prepared_update;
+	sqlite3_stmt*  m_prepared_delete;
+	
 };
 
 #endif

Modified: branches/PR-7489777/darwinup/main.cpp
===================================================================
--- branches/PR-7489777/darwinup/main.cpp	2010-02-05 21:56:15 UTC (rev 696)
+++ branches/PR-7489777/darwinup/main.cpp	2010-02-08 19:05:07 UTC (rev 697)
@@ -48,6 +48,7 @@
 
 void usage(char* progname) {
 	fprintf(stderr, "usage:    %s [-v] [-p DIR] [command] [args]          \n", progname);
+	fprintf(stderr, "version: 15                                                    \n");
 	fprintf(stderr, "                                                               \n");
 	fprintf(stderr, "options:                                                       \n");
 	fprintf(stderr, "          -f         force operation to succeed at all costs   \n");
@@ -87,6 +88,7 @@
 uint32_t verbosity;
 uint32_t force;
 
+
 int main(int argc, char* argv[]) {
 	char* progname = strdup(basename(argv[0]));      
 
@@ -144,9 +146,9 @@
 	}
 	
 	// XXX: test area for new database... remove me
-	DarwinupDatabase* testdb = new DarwinupDatabase("/.DarwinDepot/Database-V200");
+	DarwinupDatabase* testdb = depot->get_db2();
 
-	Archive* a = new Archive("/.DarwinDepot/Archives/56E93DEE-E6BB-44B2-80A4-32E961751DD8.tar.bz2");
+	Archive* a = new Archive("/.DarwinDepot/Archives/21BDC360-726B-436E-B426-B06B57F8A0CC.tar.bz2");
 	//uint64_t s = testdb->insert_archive(a->uuid(), a->info(), a->name(), a->date_installed());
 	
 	const char* mypath = "/etc/services";
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100208/e853f776/attachment-0001.html>


More information about the darwinbuild-changes mailing list