[Debtags-commits] [svn] r1855 - central-database/trunk/c-tdb

Erich Schubert erich at costa.debian.org
Tue Aug 22 21:20:29 UTC 2006


Author: erich
Date: Tue Aug 22 21:20:28 2006
New Revision: 1855

Modified:
   central-database/trunk/c-tdb/browse.cc
   central-database/trunk/c-tdb/dbid.h
   central-database/trunk/c-tdb/dbstr.h
   central-database/trunk/c-tdb/etdb.h
   central-database/trunk/c-tdb/idlist.cc
   central-database/trunk/c-tdb/idlist.h
   central-database/trunk/c-tdb/mappkgtag.cc
   central-database/trunk/c-tdb/query.cc
   central-database/trunk/c-tdb/zero-copy-serialize.h
Log:
import some API changes done when toying around with a AI-tagger written in C; note that this version of c-tdb has memory leaks

Modified: central-database/trunk/c-tdb/browse.cc
==============================================================================
--- central-database/trunk/c-tdb/browse.cc	(original)
+++ central-database/trunk/c-tdb/browse.cc	Tue Aug 22 21:20:28 2006
@@ -32,11 +32,11 @@
 			dbid entry = dbvoc.name_to_id.get(argv[i]);
 			if (!result) {
 				IdList temp = maptp.get(entry);
-				result = new IdList(temp);
+				result = new IdList(temp, true);
 			} else {
 				IdList temp = maptp.get(entry);
 				result->join(temp);
-				temp.free_data();
+				//temp.free_data();
 			}
 		} catch (etdb<dbstr,dbid>::KeyNotFoundError) {
 			cout << "Unknown tag specified: '" << argv[i] << "'" << endl;
@@ -63,7 +63,7 @@
 				} else {
 					tags[t[j]] = 1 + tags[t[j]];
 				}
-			t.free_data();
+			//t.free_data();
 		}
 		long destval = (long int) floor(result->size() * .5);
 		int bestval = 0;
@@ -82,7 +82,7 @@
 		final_tags.push_back(besttag);
 		IdList t = maptp.get(besttag);
 		result->subtract(t);
-		t.free_data();
+		//t.free_data();
 		
 	} while ( result->size() > 5 );
 	/* output */
@@ -91,7 +91,7 @@
 		try {
 			VocInfo ti = dbvoc.get(*i);
 			cout << " " << ti.name() << " " << ti.desc() << endl;
-			ti.free_data();
+			//ti.free_data();
 		} catch (etdb<dbid,VocInfo>::KeyNotFoundError e) {
 			cerr << "Unknown voc id: " << i->id << endl;
 		}
@@ -102,11 +102,12 @@
 		try {
 			PkgInfo pi = dbpkg.get((*result)[i]);
 			cout << " " << pi.name() << " " << pi.desc() << endl;
-			pi.free_data();
+			//pi.free_data();
 		} catch (etdb<dbid,PkgInfo>::KeyNotFoundError e) {
 			cerr << "Unknown pkg id: " << (*result)[i].id << endl;
 		}
 	}
-	result->free_data();
+	//result->free_data();
+	if (result) delete(result);
 	return(0);
 }

Modified: central-database/trunk/c-tdb/dbid.h
==============================================================================
--- central-database/trunk/c-tdb/dbid.h	(original)
+++ central-database/trunk/c-tdb/dbid.h	Tue Aug 22 21:20:28 2006
@@ -30,6 +30,7 @@
 	bool operator<=(const dbid& o) const { return id <= o.id; }
 	bool operator> (const dbid& o) const { return id >  o.id; }
 	bool operator>=(const dbid& o) const { return id >= o.id; }
+	dbid32 operator*() { return id; }
 };
 
 #endif

Modified: central-database/trunk/c-tdb/dbstr.h
==============================================================================
--- central-database/trunk/c-tdb/dbstr.h	(original)
+++ central-database/trunk/c-tdb/dbstr.h	Tue Aug 22 21:20:28 2006
@@ -1,24 +1,30 @@
 #ifndef DBSTR_H
 #define DBSTR_H
 #include "zero-copy-serialize.h"
+#include <string.h>
 #include <stdlib.h>
 
 class dbstr {
-public:
+private:
 	const char* data;
-
-	dbstr(const char* d) : data(d) {};
+	bool needs_free;
+public:
+	dbstr(const char* d) : data(d), needs_free(false) {};
+	dbstr(char* d, bool free) : data(d), needs_free(free) {};
 	dbstr(zcser d) {
 		data = d.dptr;
-		//d.take_data();
+		needs_free = true;
+	};
+	~dbstr() {
+		if (needs_free && data) free((char*) data);
 	};
 
-	zcser serialize() const { return zcser((char*) data, strlen(data)+1); }
+	zcser serialize() const { return zcser(data, strlen(data)+1); }
 
-/*	void free_data() {
-		assert(data);
-		free(data); data=NULL;
-	}*/
+	const char* get_data() {
+		return data;
+	}
+	const char* operator*() { return data; }
 };
 
 #endif

Modified: central-database/trunk/c-tdb/etdb.h
==============================================================================
--- central-database/trunk/c-tdb/etdb.h	(original)
+++ central-database/trunk/c-tdb/etdb.h	Tue Aug 22 21:20:28 2006
@@ -6,7 +6,6 @@
 
 #include <unistd.h>
 #include <string.h>
-#include <iostream>
 
 #include "zero-copy-serialize.h"
 
@@ -28,15 +27,15 @@
 	void close();
 
 	/* query for existence */
-	bool has_key(const K key);
+	bool has_key(const K& key);
 	/* set with given flag */
-	int set(const K k, const V v, int flag = TDB_REPLACE);
+	int set(const K& k, const V& v, int flag = TDB_REPLACE);
 	/* add won't overwrite existing values */
-	int add(const K k, const V v);
+	int add(const K& k, const V& v);
 	/* get the value of a key */
-	V get(const K k) throw(KeyNotFoundError);
+	V get(const K& k) throw(KeyNotFoundError);
 	/* add element and return next number */
-	V enumerate(const K k);
+	V enumerate(const K& k);
 
 	/* lock or unlock the database - per-key logging disappeared from latest TDB */
 	void lock();
@@ -64,7 +63,7 @@
 }
 
 template<class K, class V>
-bool etdb<K, V>::has_key(const K key) {
+bool etdb<K, V>::has_key(const K& key) {
 	zcser k = key.serialize();
 	return tdb_exists(db, (TDB_DATA)k);
 }
@@ -80,18 +79,18 @@
 }
 
 template<class K, class V>
-int etdb<K, V>::set(const K key, const V value, int flag) {
+int etdb<K, V>::set(const K& key, const V& value, int flag) {
 	zcser k = key.serialize();
 	zcser v = value.serialize();
 	return tdb_store(db, k, v, flag);
 }
 template<class K, class V>
-int etdb<K, V>::add(const K key, const V value) {
+int etdb<K, V>::add(const K& key, const V& value) {
 	return set(key, value, TDB_INSERT);
 }
 
 template<class K, class V>
-V etdb<K, V>::get(const K key) throw(KeyNotFoundError) {
+V etdb<K, V>::get(const K& key) throw(KeyNotFoundError) {
 	zcser k = key.serialize();
 	TDB_DATA v;
 	v = tdb_fetch(db, k);
@@ -101,7 +100,7 @@
 }
 
 template<class K, class V>
-V etdb<K, V>::enumerate(const K key) {
+V etdb<K, V>::enumerate(const K& key) {
 	try {
 		return get(key);
 	} catch (KeyNotFoundError k) {

Modified: central-database/trunk/c-tdb/idlist.cc
==============================================================================
--- central-database/trunk/c-tdb/idlist.cc	(original)
+++ central-database/trunk/c-tdb/idlist.cc	Tue Aug 22 21:20:28 2006
@@ -2,26 +2,48 @@
 #include <iostream>
 using namespace std;
 
+/* offset of -1 is because we have to define the idlist type with a non-empty array */
+inline size_t datasize(int size) {
+	return sizeof(idlist) + (size-1)*sizeof(dbid);
+}
+
+inline unsigned int undatasize(int datasize) {
+	return ((datasize - sizeof(idlist)) / sizeof(dbid)) + 1;
+}
+
+/* malloc a list of a given size */
+inline idlist* idlalloc(int size) {
+	return (idlist*) calloc(1, datasize(size));
+}
+
 /* create a new list with enough memory for size elements */
 IdList::IdList(size_t size) {
 	memsize = size;
-	data = (idlist*) calloc(1, sizeof(idlist) + size*sizeof(dbid));
+	data = idlalloc(size);
 	data->size = 0;
 }
 /* make a list by casting the serialization */
 IdList::IdList(zcser ser) {
 	data = (idlist*) ser.dptr;
 	assert(data);
-	memsize = (ser.dsize - sizeof(idlist)) / sizeof(dbid);
-	assert(memsize == data->size);
+	memsize = undatasize(ser.dsize);
+	assert(memsize >= data->size);
+}
+/* create a new list by duplicating */
+IdList::IdList(const IdList& list, bool copy) {
+	assert(list.data);
+	memsize = list.data->size;
+	data = idlalloc(list.data->size);
+	data->size = list.data->size;
+	memcpy(&(data->data[0]), &(list.data->data[0]), list.data->size*sizeof(dbid));
 }
 /* create a new list by inserting an element */
-IdList::IdList(IdList& list, size_t pos, dbid newid) {
+IdList::IdList(const IdList& list, size_t pos, const dbid newid) {
 	assert(list.data);
 	assert(pos <= list.data->size);
 	/* initialize with new size */
 	memsize = list.data->size + 1;
-	data = (idlist*) calloc(1, sizeof(idlist) + memsize*sizeof(dbid));
+	data = idlalloc(memsize);
 	data->size = 0;
 	/* make sure it worked */
 	assert(data);
@@ -30,18 +52,42 @@
 	memcpy(&(data->data[pos+1]), &(list.data->data[pos]), (list.data->size - pos)*sizeof(dbid));
 	data->size = list.data->size + 1;
 }
-/* duplicat a list ref */
-IdList::IdList(const IdList& list) {
-	data = list.data;
-	memsize = list.memsize;
+/* create from vector */
+IdList::IdList(const std::vector<dbid>& v) {
+	size_t l = v.size();
+	memsize = l;
+	data = idlalloc(l);
+	data->size = 0;
+	size_t pos = 0;
+	std::vector<dbid>::const_iterator i;
+	for (i=v.begin(); i != v.end(); i++) {
+		assert(pos < memsize);
+		data->data[pos] = *i;
+		pos++;
+	}
+	data->size = pos;
+}
+/* create from set */
+IdList::IdList(const std::set<dbid>& s) {
+	size_t l = s.size();
+	memsize = l;
+	data = idlalloc(l);
+	data->size = 0;
+	size_t pos = 0;
+	std::set<dbid>::const_iterator i;
+	for (i=s.begin(); i != s.end(); i++) {
+		assert(pos < memsize);
+		data->data[pos] = *i;
+		pos++;
+	}
+	data->size = pos;
 }
-/* free data */
-void IdList::free_data() {
+IdList::~IdList() {
 	free(data); data = NULL; memsize = 0;
 }
 /* serialize */
 zcser IdList::serialize() const {
-	return zcser((char*) data, sizeof(idlist) + sizeof(dbid) * data->size);
+	return zcser((char*) data, datasize(data->size));
 }
 /* accessor functions */
 size_t IdList::size() const {
@@ -105,6 +151,6 @@
 	assert(c.data);
 	IdList n(c.data->size);
 	assert(n.data);
-	memcpy(n.data, c.data, sizeof(idlist) + c.data->size * sizeof(dbid));
+	memcpy(n.data, c.data, datasize(c.data->size));
 	return n;
 }

Modified: central-database/trunk/c-tdb/idlist.h
==============================================================================
--- central-database/trunk/c-tdb/idlist.h	(original)
+++ central-database/trunk/c-tdb/idlist.h	Tue Aug 22 21:20:28 2006
@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include "dbid.h"
+#include <vector>
+#include <set>
 
 class idlist {
 public:
@@ -19,10 +21,16 @@
 	IdList(size_t size);
 	/* create (cast) list from serialization */
 	IdList(zcser ser);
+	/* create by duplicating */
+	IdList(const IdList& list, bool copy);
 	/* create by inserting an element at a given position */
-	IdList(IdList& list, size_t pos, dbid newid);
-	/* copy, not copying data */
-	IdList(const IdList& list);
+	IdList(const IdList& list, size_t pos, dbid newid);
+	/* init from vector */
+	IdList(const std::vector<dbid>& v);
+	/* init from set */
+	IdList(const std::set<dbid>& s);
+	/* destructor, freeing data */
+	~IdList();
 	
 	/* serialize */
 	zcser serialize() const;
@@ -31,8 +39,6 @@
 	size_t size() const;
 	dbid operator[](size_t pos);
 
-	/* free data */
-	void free_data();
 	/* find element */
 	int find(dbid id) const;
 	/* join with other list */

Modified: central-database/trunk/c-tdb/mappkgtag.cc
==============================================================================
--- central-database/trunk/c-tdb/mappkgtag.cc	(original)
+++ central-database/trunk/c-tdb/mappkgtag.cc	Tue Aug 22 21:20:28 2006
@@ -18,16 +18,16 @@
 			if (pos > (int) il.size()) throw "Invalid position returned by idlist_find!";
 			IdList ilneu(il, pos, value);
 			set(key, ilneu, TDB_REPLACE);
-			ilneu.free_data();
+			//ilneu.free_data();
 		} else
 			cout << "Value " << value.id << " found at position " << pos << " in key " << key.id << endl;
-		il.free_data();
+		//il.free_data();
 		unlock();
 	} catch (KeyNotFoundError e) {
 		IdList il(1);
 		il.data->data[0] = value;
 		il.data->size = 1;
 		set(key, il, TDB_INSERT);
-		il.free_data();
+		//il.free_data();
 	}
 }

Modified: central-database/trunk/c-tdb/query.cc
==============================================================================
--- central-database/trunk/c-tdb/query.cc	(original)
+++ central-database/trunk/c-tdb/query.cc	Tue Aug 22 21:20:28 2006
@@ -20,11 +20,11 @@
 			dbid entry = dbvoc.name_to_id.get(argv[i]);
 			if (!result) {
 				IdList temp = maptp.get(entry);
-				result = new IdList(temp);
+				result = new IdList(temp, true);
 			} else {
 				IdList temp = maptp.get(entry);
 				result->join(temp);
-				temp.free_data();
+				//temp.free_data();
 			}
 		} catch (etdb<dbstr,dbid>::KeyNotFoundError e) {
 			cout << "Unknown tag specified: '" << argv[i] << "'" << endl;
@@ -40,12 +40,12 @@
 			PkgInfo pi = dbpkg.get((*result)[i]);
 			if (i > 0) cout << ", ";
 			cout << pi.name();
-			pi.free_data();
+			//pi.free_data();
 		} catch (etdb<dbid,PkgInfo>::KeyNotFoundError e) {
 			//cout << "A package was listed without package information being available!" << endl;
 		}
 	}
 	cout << endl;
-	result->free_data();
+	if (result) delete result;
 	return(0);
 }

Modified: central-database/trunk/c-tdb/zero-copy-serialize.h
==============================================================================
--- central-database/trunk/c-tdb/zero-copy-serialize.h	(original)
+++ central-database/trunk/c-tdb/zero-copy-serialize.h	Tue Aug 22 21:20:28 2006
@@ -2,6 +2,7 @@
 #define ZERO_COPY_SERIALIZE_H
 #include <tdb.h>
 #include <assert.h>
+#include <stdlib.h>
 
 class zcser : public TDB_DATA {
 public:
@@ -9,7 +10,7 @@
 	/*char*	dptr;
 	size_t	dsize;*/
 
-	zcser(void* d, size_t s) {
+	zcser(const void* d, size_t s) {
 		dptr = (char*)d;
 		dsize = s;
 	};



More information about the Debtags-commits mailing list