[darwinbuild-changes] [706] branches/PR-7489777/darwinup
source_changes at macosforge.org
source_changes at macosforge.org
Thu Feb 18 16:30:43 PST 2010
Revision: 706
http://trac.macosforge.org/projects/darwinbuild/changeset/706
Author: wsiegrist at apple.com
Date: 2010-02-18 16:30:41 -0800 (Thu, 18 Feb 2010)
Log Message:
-----------
Move Archive* instantiation to DarwinupDatabase, add offset data to Table and Column, fix free_row by memcpying the pointer instead of trying to cast to it
Modified Paths:
--------------
branches/PR-7489777/darwinup/Archive.h
branches/PR-7489777/darwinup/Column.cpp
branches/PR-7489777/darwinup/Column.h
branches/PR-7489777/darwinup/DB.cpp
branches/PR-7489777/darwinup/DB.h
branches/PR-7489777/darwinup/Database.cpp
branches/PR-7489777/darwinup/Depot.cpp
branches/PR-7489777/darwinup/Table.cpp
branches/PR-7489777/darwinup/Table.h
branches/PR-7489777/darwinup/main.cpp
Modified: branches/PR-7489777/darwinup/Archive.h
===================================================================
--- branches/PR-7489777/darwinup/Archive.h 2010-02-18 21:34:36 UTC (rev 705)
+++ branches/PR-7489777/darwinup/Archive.h 2010-02-19 00:30:41 UTC (rev 706)
@@ -134,6 +134,7 @@
time_t m_date_installed;
friend struct Depot;
+ friend struct DarwinupDatabase;
};
Modified: branches/PR-7489777/darwinup/Column.cpp
===================================================================
--- branches/PR-7489777/darwinup/Column.cpp 2010-02-18 21:34:36 UTC (rev 705)
+++ branches/PR-7489777/darwinup/Column.cpp 2010-02-19 00:30:41 UTC (rev 706)
@@ -100,6 +100,10 @@
return sizeof(void*);
}
+int Column::offset() {
+ return m_offset;
+}
+
const bool Column::is_index() {
return m_is_index;
}
Modified: branches/PR-7489777/darwinup/Column.h
===================================================================
--- branches/PR-7489777/darwinup/Column.h 2010-02-18 21:34:36 UTC (rev 705)
+++ branches/PR-7489777/darwinup/Column.h 2010-02-19 00:30:41 UTC (rev 706)
@@ -50,7 +50,9 @@
uint32_t size();
const char* create();
const char* index();
+ int offset();
+
const bool is_index();
const bool is_pk();
const bool is_unique();
@@ -62,6 +64,9 @@
bool m_is_index;
bool m_is_pk;
bool m_is_unique;
+ int m_offset;
+
+ friend struct Table;
};
#endif
Modified: branches/PR-7489777/darwinup/DB.cpp
===================================================================
--- branches/PR-7489777/darwinup/DB.cpp 2010-02-18 21:34:36 UTC (rev 705)
+++ branches/PR-7489777/darwinup/DB.cpp 2010-02-19 00:30:41 UTC (rev 706)
@@ -251,26 +251,22 @@
}
-// serial uuid name date_added active info
+Archive* DarwinupDatabase::make_archive(uint8_t* data) {
+ uint64_t serial;
+ memcpy(&serial, &data[this->archive_offset(0)], sizeof(uint64_t));
+ uuid_t* uuid;
+ memcpy(&uuid, &data[this->archive_offset(1)], sizeof(uuid_t*));
+ char* name;
+ memcpy(&name, &data[this->archive_offset(2)], sizeof(char*));
+ time_t date_added;
+ memcpy(&date_added, &data[this->archive_offset(3)], sizeof(time_t));
+ uint64_t info;
+ memcpy(&info, &data[this->archive_offset(5)], sizeof(uint64_t));
-/*
-int DarwinupDatabase::process_archive_results() {
- const unsigned char* name = sqlite3_column_text(stmt, 1);
- uuid_t uuid;
- const void* blob = sqlite3_column_blob(stmt, 1);
- int blobsize = sqlite3_column_bytes(stmt, 1);
- if (blobsize > 0) {
- assert(blobsize == sizeof(uuid_t));
- memcpy(uuid, blob, sizeof(uuid_t));
- } else {
- uuid_clear(uuid);
- }
- uint64_t serial = sqlite3_column_int64(stmt, 0);
- uint64_t info = sqlite3_column_int64(stmt, 2);
- time_t date_added = sqlite3_column_int(stmt, 3);
- return 0;
+ Archive* archive = new Archive(serial, *uuid, name, NULL, info, date_added);
+ this->m_archives_table->free_result(data);
+ return archive;
}
-*/
int DarwinupDatabase::get_archive(uint8_t** data, uuid_t uuid) {
return this->get_row("archive__uuid",
@@ -300,7 +296,7 @@
}
int DarwinupDatabase::archive_offset(int column) {
- return column*8;
+ return this->m_archives_table->offset(column);
}
Modified: branches/PR-7489777/darwinup/DB.h
===================================================================
--- branches/PR-7489777/darwinup/DB.h 2010-02-18 21:34:36 UTC (rev 705)
+++ branches/PR-7489777/darwinup/DB.h 2010-02-19 00:30:41 UTC (rev 706)
@@ -61,6 +61,7 @@
uint64_t count_files(Archive* archive, const char* path);
// Archives
+ Archive* make_archive(uint8_t* data);
int get_archive(uint8_t** data, uuid_t uuid);
int get_archive(uint8_t** data, uint64_t serial);
int get_archive(uint8_t** data, const char* name);
Modified: branches/PR-7489777/darwinup/Database.cpp
===================================================================
--- branches/PR-7489777/darwinup/Database.cpp 2010-02-18 21:34:36 UTC (rev 705)
+++ branches/PR-7489777/darwinup/Database.cpp 2010-02-19 00:30:41 UTC (rev 706)
@@ -243,7 +243,6 @@
char* error;
if (this->m_db) {
char *query = sqlite3_vmprintf(fmt, args);
- IF_DEBUG("[DATABASE] SQL(): %s \n", query);
res = sqlite3_exec(this->m_db, query, NULL, NULL, &error);
sqlite3_free(query);
} else {
Modified: branches/PR-7489777/darwinup/Depot.cpp
===================================================================
--- branches/PR-7489777/darwinup/Depot.cpp 2010-02-18 21:34:36 UTC (rev 705)
+++ branches/PR-7489777/darwinup/Depot.cpp 2010-02-19 00:30:41 UTC (rev 706)
@@ -145,63 +145,26 @@
// Unserialize an archive from the database.
// Find the archive by UUID.
-// XXX: should be memoized
Archive* Depot::archive(uuid_t uuid) {
int res = 0;
Archive* archive = NULL;
- static sqlite3_stmt* stmt = NULL;
- if (stmt == NULL && m_db) {
- const char* query = "SELECT serial, name, info, date_added FROM archives WHERE uuid=?";
- 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_blob(stmt, 1, uuid, sizeof(uuid_t), SQLITE_STATIC);
- if (res == 0) res = sqlite3_step(stmt);
- if (res == SQLITE_ROW) {
- uint64_t serial = sqlite3_column_int64(stmt, 0);
- const unsigned char* name = sqlite3_column_text(stmt, 1);
- uint64_t info = sqlite3_column_int64(stmt, 2);
- time_t date_added = sqlite3_column_int(stmt, 3);
- archive = new Archive(serial, uuid, (const char*)name, NULL, info, date_added);
- }
- sqlite3_reset(stmt);
- }
+ uint8_t* data;
+
+ res = this->m_db2->get_archive(&data, uuid);
+ if (res == 0) archive = this->m_db2->make_archive(data);
return archive;
}
// Unserialize an archive from the database.
// Find the archive by serial.
-// XXX: should be memoized
Archive* Depot::archive(uint64_t serial) {
int res = 0;
Archive* archive = NULL;
- static sqlite3_stmt* stmt = NULL;
- if (stmt == NULL && m_db) {
- const char* query = "SELECT uuid, name, info, date_added FROM archives WHERE serial=?";
- 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, serial);
- if (res == 0) res = sqlite3_step(stmt);
- if (res == SQLITE_ROW) {
- uuid_t uuid;
- const void* blob = sqlite3_column_blob(stmt, 0);
- int blobsize = sqlite3_column_bytes(stmt, 0);
- if (blobsize > 0) {
- assert(blobsize == sizeof(uuid_t));
- memcpy(uuid, blob, sizeof(uuid_t));
- } else {
- uuid_clear(uuid);
- }
- const unsigned char* name = sqlite3_column_text(stmt, 1);
- uint64_t info = sqlite3_column_int64(stmt, 2);
- time_t date_added = sqlite3_column_int(stmt, 3);
- archive = new Archive(serial, uuid, (const char*)name, NULL, info, date_added);
- }
- sqlite3_reset(stmt);
- }
+ uint8_t* data;
+
+ res = this->m_db2->get_archive(&data, serial);
+ if (res == 0) archive = this->m_db2->make_archive(data);
+
return archive;
}
@@ -210,32 +173,10 @@
Archive* Depot::archive(archive_name_t name) {
int res = 0;
Archive* archive = NULL;
- static sqlite3_stmt* stmt = NULL;
- if (stmt == NULL && m_db) {
- const char* query = "SELECT serial, uuid, info, date_added FROM archives WHERE name=? ORDER BY date_added DESC LIMIT 1";
- 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_text(stmt, 1, name, -1, SQLITE_STATIC);
- if (res == 0) res = sqlite3_step(stmt);
- if (res == SQLITE_ROW) {
- uuid_t uuid;
- const void* blob = sqlite3_column_blob(stmt, 1);
- int blobsize = sqlite3_column_bytes(stmt, 1);
- if (blobsize > 0) {
- assert(blobsize == sizeof(uuid_t));
- memcpy(uuid, blob, sizeof(uuid_t));
- } else {
- uuid_clear(uuid);
- }
- uint64_t serial = sqlite3_column_int64(stmt, 0);
- uint64_t info = sqlite3_column_int64(stmt, 2);
- time_t date_added = sqlite3_column_int(stmt, 3);
- archive = new Archive(serial, uuid, (const char*)name, NULL, info, date_added);
- }
- sqlite3_reset(stmt);
- }
+ uint8_t* data;
+
+ res = this->m_db2->get_archive(&data, name);
+ if (res == 0) archive = this->m_db2->make_archive(data);
return archive;
}
Modified: branches/PR-7489777/darwinup/Table.cpp
===================================================================
--- branches/PR-7489777/darwinup/Table.cpp 2010-02-18 21:34:36 UTC (rev 705)
+++ branches/PR-7489777/darwinup/Table.cpp 2010-02-19 00:30:41 UTC (rev 706)
@@ -35,6 +35,8 @@
#include <stdlib.h>
#include "Table.h"
+// how much we grow by when we need more space
+#define REALLOC_FACTOR 4
// XXX
void __hex_str(const char* s) {
@@ -122,28 +124,37 @@
int Table::add_column(Column* c) {
+ // accumulate offsets for columns
+ static int offset = 0;
+ c->m_offset = offset;
+ offset += c->size();
+
+ // reallocate if needed
if (m_column_count >= m_column_max) {
- m_columns = (Column**)realloc(m_columns, m_column_max * sizeof(Column*) * 4);
+ m_columns = (Column**)realloc(m_columns, m_column_max * sizeof(Column*) * REALLOC_FACTOR);
if (!m_columns) {
fprintf(stderr, "Error: unable to reallocate memory to add a column\n");
return 1;
}
- m_column_max *= 4;
+ m_column_max *= REALLOC_FACTOR;
}
m_columns[m_column_count++] = c;
return 0;
}
+int Table::offset(int column) {
+ return this->m_columns[column]->offset();
+}
uint8_t* Table::alloc_result() {
if (m_result_count >= m_result_max) {
- m_results = (uint8_t**)realloc(m_results, m_result_max * sizeof(uint8_t*) * 4);
+ m_results = (uint8_t**)realloc(m_results, m_result_max * sizeof(uint8_t*) * REALLOC_FACTOR);
if (!m_results) {
fprintf(stderr, "Error: unable to reallocate memory to add a result row\n");
return NULL;
}
- m_result_max *= 4;
+ m_result_max *= REALLOC_FACTOR;
}
m_result_count++;
m_results[m_result_count-1] = (uint8_t*)calloc(1, this->row_size());
@@ -152,6 +163,7 @@
int Table::free_row(uint8_t* row) {
uint8_t* current = row;
+ void* ptr;
for (uint32_t i=0; i < m_column_count; i++) {
switch (m_columns[i]->type()) {
case SQLITE_INTEGER:
@@ -159,7 +171,8 @@
// nothing to free
break;
default:
- free(current);
+ memcpy(&ptr, current, sizeof(void*));
+ free(ptr);
current += sizeof(void*);
}
}
@@ -277,7 +290,6 @@
sqlite3_stmt* Table::count(sqlite3* db) {
- IF_DEBUG("[TABLE] entering count of %s \n", m_name);
sqlite3_stmt* stmt = (sqlite3_stmt*)malloc(sizeof(sqlite3_stmt*));
char* query;
int size = asprintf(&query, "SELECT count(*) FROM %s ;", m_name) + 1;
@@ -292,7 +304,6 @@
__check_and_cat(" WHERE 1");
__where_va_columns;
strlcat(query, ";", size);
- IF_DEBUG("[TABLE] count query: %s \n", query);
__prepare_stmt;
return stmt;
@@ -307,7 +318,6 @@
__check_and_cat(" WHERE 1");
__where_va_columns;
strlcat(query, ";", size);
- IF_DEBUG("[TABLE] get_column query: %s \n", query);
__prepare_stmt;
return stmt;
@@ -335,7 +345,6 @@
__check_and_cat("=? WHERE 1");
__where_va_columns;
strlcat(query, ";", size);
- IF_DEBUG("[TABLE] update_value query: %s \n", query);
__prepare_stmt;
return stmt;
@@ -521,7 +530,6 @@
__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-18 21:34:36 UTC (rev 705)
+++ branches/PR-7489777/darwinup/Table.h 2010-02-19 00:30:41 UTC (rev 706)
@@ -38,7 +38,6 @@
#include <sqlite3.h>
#include "Column.h"
-
struct Table {
Table();
Table(const char* name);
@@ -48,6 +47,7 @@
const Column** columns();
Column* column(uint32_t index);
uint32_t column_count();
+ int offset(int column);
uint8_t* alloc_result();
int free_result(uint8_t* result);
@@ -56,8 +56,7 @@
uint32_t row_size();
// return SQL statements for this table
- char* create();
- char* drop(); // XXX: implement this
+ char* create();
sqlite3_stmt* count(sqlite3* db);
sqlite3_stmt* count(sqlite3* db, uint32_t count, va_list args);
@@ -94,6 +93,7 @@
uint32_t m_result_count;
uint32_t m_result_max;
+ friend struct Database;
};
#endif
Modified: branches/PR-7489777/darwinup/main.cpp
===================================================================
--- branches/PR-7489777/darwinup/main.cpp 2010-02-18 21:34:36 UTC (rev 705)
+++ branches/PR-7489777/darwinup/main.cpp 2010-02-19 00:30:41 UTC (rev 706)
@@ -173,89 +173,8 @@
// XXX: test area for new database... remove me
- DarwinupDatabase* testdb = depot->get_db2();
+ //DarwinupDatabase* testdb = depot->get_db2();
- uuid_t uuid;
- uuid_parse("5A41995B-CA3F-4BF3-84AC-2F5A42357769", uuid);
- uint8_t* data;
- fprintf(stderr, "main: data %p uuid %14s \n", &data, uuid);
- testdb->get_archive(&data, uuid);
- fprintf(stderr, "RESULT: %llu \n", (uint64_t)data[0]);
- free(data);
- uuid_clear(uuid);
-
- testdb->get_archive(&data, 42);
- fprintf(stderr, "DATA: %p \n", data);
- __data_hex(data, 48);
- char* p;
- memcpy(&p, &data[16], 8);
- uint64_t ss = 0;
- memcpy(&ss, &data[0], 8);
- time_t t = 0;
- memcpy(&t, &data[24], 8);
-
- fprintf(stderr, "RESULT: %llu \n", ss);
- fprintf(stderr, "RESULT: %p -> %s \n", p, p);
- fprintf(stderr, "RESULT: %llu \n", (uint64_t)t);
-
-
-
- testdb->get_archive(&data, "root2");
- char ustr[37];
- uuid_t* up;
- memcpy(&up, &data[8], sizeof(uuid_t*));
- uuid_unparse_upper(*up, ustr);
- fprintf(stderr, "RESULT: %s \n", ustr);
-
-
-
-
- uint64_t* s;
- uint64_t a_serial = 3;
- Archive* a = depot->archive(a_serial);
- res = testdb->get_file_serial_from_archive(a, "/e/ee/e_data.txt", &s);
- if (!res) {
- IF_DEBUG("s = %llu \n", *s);
- } else {
- IF_DEBUG("DID NOT FIND SERIAL\n");
- }
-
- uint64_t* serials;
- uint32_t sc;
- testdb->get_file_serials(&serials, &sc);
- IF_DEBUG("serials(%p) = %llu sc = %u \n", serials, serials[0], sc);
- for (uint32_t i=0; i < sc; i++) {
- fprintf(stderr, "TEST: %d = %llu \n", i, serials[i]);
- }
-
- testdb->get_inactive_archive_serials(&serials, &sc);
- IF_DEBUG("serials(%p) = %llu sc = %u \n", serials, serials[0], sc);
- for (uint32_t i=0; i < sc; i++) {
- fprintf(stderr, "INACTIVE: %d = %llu \n", i, serials[i]);
- }
-
- /*
- 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";
- File* f = FileFactory(mypath);
- testdb->insert_file(1, 2, 3, 4, f->digest(), a, mypath);
- testdb->update_file(a, mypath, 5, 6, 7, 8, f->digest());
- testdb->update_file(a, mypath, 6, 7, 8, 9, f->digest());
-
- if (depot->has_file(a, f)) {
- fprintf(stderr, "HASFILE: true\n");
- }
- if (depot->has_file(a, f)) {
- fprintf(stderr, "HASFILE: true\n");
- }
- if (depot->has_file(a, f)) {
- fprintf(stderr, "HASFILE: true\n");
- }
- */
-
- exit(0);
// XXX
if (argc == 2 && strcmp(argv[0], "install") == 0) {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100218/9041f41c/attachment-0001.html>
More information about the darwinbuild-changes
mailing list