Monotone-Parent: 89b88978ce7a84af4dd6e2e230ede1c3ef700068

Monotone-Revision: c29fcc5ce939d41d4b8a3795a566530292ec2f0f

Monotone-Author: wsourdeau@inverse.ca
Monotone-Date: 2009-03-17T15:58:25
Monotone-Branch: ca.inverse.sogo
maint-2.0.2
Wolfgang Sourdeau 2009-03-17 15:58:25 +00:00
parent b65ae175c3
commit 57830dbb88
2 changed files with 191 additions and 172 deletions

View File

@ -1,3 +1,11 @@
2009-03-17 Wolfgang Sourdeau <wsourdeau@inverse.ca>
* GCSFolder.m ([GCSFolder
-writeContent:_contenttoName:_namebaseVersion:_baseVersion]):
reorganized method to centralize error management, enabling us to
remove the "CHECKERROR" macro. The method was also not closing
channels whenever an error occured.
2008-09-22 Wolfgang Sourdeau <wsourdeau@inverse.ca>
* GCSFolder.m ([GCSFolder

View File

@ -50,15 +50,6 @@ typedef enum {
bothTableRequired = 3
} GCSTableRequirement;
#define CHECKERROR() \
if (error) { \
[[storeChannel adaptorContext] rollbackTransaction]; \
[[quickChannel adaptorContext] rollbackTransaction]; \
[self logWithFormat:@"ERROR(%s): cannot %s content : %@", \
__PRETTY_FUNCTION__, isNewRecord ? "insert" : "update", error]; \
return error; \
} \
@implementation GCSFolder
static BOOL debugOn = NO;
@ -815,26 +806,19 @@ static NSArray *contentFieldNames = nil;
NSMutableDictionary *quickRow, *contentRow;
NSDictionary *currentRow;
GCSFieldExtractor *extractor;
NSException *error;
NSNumber *storedVersion;
BOOL isNewRecord, hasInsertDelegate, hasUpdateDelegate;
NSCalendarDate *nowDate;
NSNumber *now;
EOEntity *quickTableEntity, *storeTableEntity;
NSArray *rows;
NSException *error;
/* check preconditions */
if (_name == nil) {
return [NSException exceptionWithName:@"GCSStoreException"
reason:@"no content filename was provided"
userInfo:nil];
}
if (_content == nil) {
return [NSException exceptionWithName:@"GCSStoreException"
reason:@"no content was provided"
userInfo:nil];
}
if (_name)
{
if (_content)
{
/* run */
error = nil;
nowDate = [NSCalendarDate date];
@ -872,22 +856,15 @@ static NSArray *contentFieldNames = nil;
}
/* check whether sequence matches */
if (_baseVersion != 0 /* use 0 to override check */) {
if (_baseVersion != [storedVersion unsignedIntValue]) {
/* version mismatch (concurrent update) */
return [self errorVersionMismatchBetweenStoredVersion:
[storedVersion unsignedIntValue]
andExpectedVersion:_baseVersion];
}
}
/* use version = 0 to override check */
if (_baseVersion == 0
|| _baseVersion == [storedVersion unsignedIntValue])
{
/* extract quick info */
extractor = [folderInfo quickExtractor];
if ((quickRow = [extractor extractQuickFieldsFromContent:_content]) == nil) {
return [self errorExtractorReturnedNoQuickRow:extractor
forContent:_content];
}
quickRow = [extractor extractQuickFieldsFromContent:_content];
if (quickRow)
{
[quickRow setObject:_name forKey:@"c_name"];
if (doLogStore)
@ -900,26 +877,28 @@ static NSArray *contentFieldNames = nil;
[contentRow addEntriesFromDictionary:quickRow];
[contentRow setObject:_name forKey:@"c_name"];
if (isNewRecord) [contentRow setObject:now forKey:@"c_creationdate"];
[contentRow setObject:now forKey:@"c_lastmodified"];
if (isNewRecord)
[contentRow setObject:[NSNumber numberWithInt:0] forKey:@"c_version"];
else {
// TODO: increase version?
{
[contentRow setObject:now forKey:@"c_creationdate"];
[contentRow setObject:[NSNumber numberWithInt:0]
forKey:@"c_version"];
}
else // TODO: increase version?
[contentRow setObject:
[NSNumber numberWithInt:([storedVersion intValue] + 1)]
forKey:@"c_version"];
}
[contentRow setObject:_content forKey:@"c_content"];
/* open channels */
if ((storeChannel = [self acquireStoreChannel]) == nil) {
[self errorWithFormat:@"%s: could not open storage channel!",
__PRETTY_FUNCTION__];
return nil;
}
if (!ofFlags.sameTableForQuick) {
if ((quickChannel = [self acquireQuickChannel]) == nil) {
storeChannel = [self acquireStoreChannel];
if (storeChannel)
{
if (!ofFlags.sameTableForQuick)
{
quickChannel = [self acquireQuickChannel];
if (!quickChannel)
{
[self errorWithFormat:@"%s: could not open quick channel!",
__PRETTY_FUNCTION__];
[self releaseChannel:storeChannel];
@ -941,26 +920,25 @@ static NSArray *contentFieldNames = nil;
quickTableEntity = [self _quickTableEntity];
storeTableEntity = [self _storeTableEntity];
if (isNewRecord) {
if (!ofFlags.sameTableForQuick) {
if (isNewRecord)
{
if (!ofFlags.sameTableForQuick)
error = (hasInsertDelegate
? [quickChannel insertRowX: quickRow forEntity: quickTableEntity]
: [quickChannel
evaluateExpressionX: [self _generateInsertStatementForRow: quickRow
tableName: [self quickTableName]]]);
CHECKERROR();
}
if (!error)
error = (hasInsertDelegate
? [storeChannel insertRowX: contentRow forEntity: storeTableEntity]
: [storeChannel
evaluateExpressionX: [self _generateInsertStatementForRow: contentRow
tableName: [self storeTableName]]]);
CHECKERROR();
}
else {
if (!ofFlags.sameTableForQuick) {
else
{
if (!ofFlags.sameTableForQuick)
error = (hasUpdateDelegate
? [quickChannel updateRowX: quickRow
describedByQualifier: [self _qualifierUsingWhereColumn: @"c_name"
@ -970,9 +948,7 @@ static NSArray *contentFieldNames = nil;
tableName: [self quickTableName]
whereColumn: @"c_name" isEqualTo: _name
andColumn: nil isEqualTo: nil]]);
CHECKERROR();
}
if (!error)
error = (hasUpdateDelegate
? [storeChannel updateRowX: contentRow
describedByQualifier: [self _qualifierUsingWhereColumn: @"c_name" isEqualTo: _name
@ -983,14 +959,49 @@ static NSArray *contentFieldNames = nil;
whereColumn: @"c_name" isEqualTo: _name
andColumn: (_baseVersion != 0 ? (id)@"c_version" : (id)nil)
isEqualTo: (_baseVersion != 0 ? [NSNumber numberWithUnsignedInt: _baseVersion] : (NSNumber *)nil)]]);
CHECKERROR();
}
if (error)
{
[[storeChannel adaptorContext] rollbackTransaction];
[[quickChannel adaptorContext] rollbackTransaction];
[self logWithFormat:
@"ERROR(%s): cannot %s content : %@", __PRETTY_FUNCTION__,
isNewRecord ? "insert" : "update",
error];
}
else
{
[[storeChannel adaptorContext] commitTransaction];
[[quickChannel adaptorContext] commitTransaction];
}
[self releaseChannel: storeChannel];
if (!ofFlags.sameTableForQuick) [self releaseChannel: quickChannel];
if (!ofFlags.sameTableForQuick)
[self releaseChannel: quickChannel];
}
else
[self errorWithFormat:@"%s: could not open storage channel!",
__PRETTY_FUNCTION__];
}
else
error = [self errorExtractorReturnedNoQuickRow:extractor
forContent:_content];
}
else /* version mismatch (concurrent update) */
error = [self errorVersionMismatchBetweenStoredVersion:
[storedVersion unsignedIntValue]
andExpectedVersion: _baseVersion];
}
else
error = [NSException exceptionWithName:@"GCSStoreException"
reason:@"no content was provided"
userInfo:nil];
}
else
error = [NSException exceptionWithName:@"GCSStoreException"
reason:@"no content filename was provided"
userInfo:nil];
return error;
}