[commit] r1279 - trunk/GME/Core

GMESRC Repository Notifications gme-commit at list.isis.vanderbilt.edu
Mon Apr 25 14:41:09 CDT 2011


Author: ksmyth
Date: Mon Apr 25 14:41:08 2011
New Revision: 1279

Log:
Lazy load string attributes

Modified:
   trunk/GME/Core/CoreBinFile.cpp
   trunk/GME/Core/CoreBinFile.h

Modified: trunk/GME/Core/CoreBinFile.cpp
==============================================================================
--- trunk/GME/Core/CoreBinFile.cpp	Mon Apr 25 09:54:50 2011	(r1278)
+++ trunk/GME/Core/CoreBinFile.cpp	Mon Apr 25 14:41:08 2011	(r1279)
@@ -615,11 +615,26 @@
 	}
 }
 
+void CCoreBinFile::readstring(char*& pos) {
+	pos = cifs;
+	int len;
+
+	read(len);
+	cifs += len;
+}
+
 void CCoreBinFile::read(CComBstrObj &ss)
 {
+	read(ss, cifs);
+}
+
+void CCoreBinFile::read(CComBstrObj &ss, char*& cifs)
+{
 	int len;
 
-	read(len);
+	//read(len);
+	// use local cifs
+	CoreBinFile_read(len, 4);
 	ASSERT( len >= 0 );
 
 	if( len > 0 ) {
@@ -679,6 +694,15 @@
 	delete[] s;
 }
 
+void CCoreBinFile::writestring(const char* pos)
+{
+	int len;
+	memcpy(&len, pos, sizeof(len));
+	write(len);
+	ofs.write(pos+sizeof(int), len);
+}
+
+
 // ------- Attribute
 
 STDMETHODIMP CCoreBinFile::get_AttributeValue(VARIANT *p)
@@ -882,6 +906,10 @@
 	ASSERT( !ofs.is_open() );
 	ASSERT( metaprojectid.size() == 16 );
 
+	// FIXME: KMS: it could happen that the save fails, and the user loses data. (at least this isn't worse than previous versions)
+	HANDLE hFile = CreateFileA(filename.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL);
+	CloseHandle(hFile);
+
 	ofs.clear();
 	ofs.open(filename.c_str(), std::ios::out | std::ios::binary);
 	if( ofs.fail() || !ofs.is_open() ) {
@@ -923,8 +951,6 @@
 {
 	InitMaxObjIDs();
 
-	{
-	membuf file_buffer;
 	if (file_buffer.open(filename.c_str()) != 0) {
 		HR_THROW(HRESULT_FROM_WIN32(GetLastError()));
 	}
@@ -1009,13 +1035,10 @@
 
 	isEmpty = true;
 	resolvelist.clear();
-	}
 
 	ofs.clear();
-	ofs.open(filename.c_str(), std::ios::app | std::ios::binary);
+	  // FIXME: set read_only correctly
 	read_only = false;
-	if( ofs.fail() || !ofs.is_open() )	read_only = true;
-	else ofs.close();
 }
 
 STDMETHODIMP CCoreBinFile::OpenProject(BSTR connection, VARIANT_BOOL *ro_mode) {

Modified: trunk/GME/Core/CoreBinFile.h
==============================================================================
--- trunk/GME/Core/CoreBinFile.h	Mon Apr 25 09:54:50 2011	(r1278)
+++ trunk/GME/Core/CoreBinFile.h	Mon Apr 25 14:41:08 2011	(r1279)
@@ -17,7 +17,7 @@
 		{ }
 	
 	int open(const char* filename) {
-		hFile = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
+		hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
 		if (hFile == INVALID_HANDLE_VALUE) {
 			return 1;
 		}
@@ -34,6 +34,10 @@
 			return 1;
 		}
 		end = begin + filesize;
+		CloseHandle(hFile);
+		hFile = INVALID_HANDLE_VALUE;
+		CloseHandle(hFileMappingObject);
+		hFileMappingObject = INVALID_HANDLE_VALUE;
 		return 0;
 	}
 
@@ -249,8 +253,10 @@
 	void read(long &a)  { CoreBinFile_read(a, sizeof(long)); }
 	void read(double &a)  { CoreBinFile_read(a, sizeof(double)); }
 	void read(CComBstrObj &a);
+	void read(CComBstrObj &ss, char*& cifs);
 	void read(bindata &a);
 	void read(unsigned char*& b, int& len);
+	void readstring(char*& pos);
 
 	void write(unsigned char a) { ofs.write((const char*)&a, sizeof(unsigned char)); }
 	void write(short a) { ofs.write((const char*)&a, sizeof(short)); }
@@ -260,7 +266,7 @@
 	void write(const CComBstrObj &a);
 	void write(const bindata &a);
 	void write(const unsigned char* a, int len);
-
+	void writestring(const char* pos);
 // ------- Attribute
 
 public:
@@ -314,6 +320,7 @@
 	bool intrans;
 	bool modified;
 
+	membuf file_buffer;
 	bool IsOpened() const { return !filename.empty(); }
 	bool InTransaction() const { return intrans; }
 
@@ -388,18 +395,44 @@
 class BinAttr<VALTYPE_STRING> : public BinAttrBase
 {
 public:
-	BinAttr() { }
+	BinAttr() : pos(NULL) { }
 
 	CComBstrObj a;
+	// Lazy read: if pos is non-null, a is invalid and 
+	//    pos points to the pascal-style UTF-8 string in the memory-mapped mga file
+	char* pos;
 
 	virtual valtype_type GetValType() const NOTHROW { return VALTYPE_STRING; }
 	virtual void Set(CCoreBinFile *binfile, VARIANT p)
-	{ ASSERT( binfile != NULL ); binfile->modified = true; CopyTo(p, a); }
+	{ 
+		ASSERT( binfile != NULL ); 
+		binfile->modified = true; 
+		CopyTo(p, a);
+		pos = NULL;
+	}
 
-	virtual void Get(CCoreBinFile *binfile, VARIANT *p) const { CopyTo(a, p); }
-	virtual void Write(CCoreBinFile *binfile) const { binfile->write(a); }
-	virtual void Read(CCoreBinFile *binfile) { binfile->read(a); }
-	BinAttr(BinAttr<VALTYPE_STRING>&& that) : BinAttrBase(that.attrid), a(std::move(that.a)) { }
+	virtual void Get(CCoreBinFile *binfile, VARIANT *p) const {
+		if (pos != NULL) {
+			BinAttr<VALTYPE_STRING>* this_ = const_cast<BinAttr<VALTYPE_STRING>*>(this);
+			binfile->read(this_->a, this_->pos);
+			this_->pos = NULL;
+		}
+		CopyTo(a, p);
+	}
+	virtual void Write(CCoreBinFile *binfile) const {
+		if (pos == NULL) {
+			binfile->write(a);
+		} else {
+			binfile->writestring(pos);
+		}
+	}
+	virtual void Read(CCoreBinFile *binfile) {
+		binfile->readstring(pos);
+		// to disable lazy read:
+		//binfile->read(a, pos);
+		//pos = NULL;
+	}
+	BinAttr(BinAttr<VALTYPE_STRING>&& that) : BinAttrBase(that.attrid), a(std::move(that.a)), pos(that.pos) { }
 	virtual void move(BinAttrUnion&& dest) {
 		new (&dest) BinAttr<VALTYPE_STRING>(std::move(*this));
 	}


More information about the gme-commit mailing list