|
*************************** SDStore.m ******************************
///////////////////////////////////////////////////////////////////////////
//
// SDStore
// kaj: Jun 4 1997
// Purpose: cover for IXStore methods
//
// The only IXStore methods we used were for transaction management
// SDStore does not implement transaction rollback; when
// commitTransaction is called, the whole file is saved.
//
// Implementation: map block numbers to NSData objects that hold
// whatever is being stored in the SDStore.
//
///////////////////////////////////////////////////////////////////////////
#import "ixCover.h"
@implementation SDStore:NSObject
#define BLOCK_NUMBER_KEY @"BlockNumberKey"
NSString *SDStoreTransactionWasCommitted = @"SDStoreTransactionWasCommitted";
///////////////////////////////////////////////////////////////////////////
//
// IXStore covers
//
///////////////////////////////////////////////////////////////////////////
- (id) init
{
[super init];
rootDictionary = nil;
privateInfo = [[NSMutableDictionary allocWithZone:[self zone]] initWithCapacity:1];
clientList = [[NSMutableArray allocWithZone:[self zone]] initWithCapacity:1];
lastBlock = 0;
[self recordLastBlockNumber];
return self;
}
- (void) createBlock:(int *)blockNumber withSize:(unsigned int)aSize
{
// if we're creating a block, we'll need a rootDictionary to store the contents
if (!rootDictionary) {
rootDictionary = [[NSMutableDictionary allocWithZone:[self zone]] initWithCapacity:1];
}
*blockNumber = ++lastBlock;
[self recordLastBlockNumber];
}
- (unsigned int) startTransaction
{
transactionLevel++;
return transactionLevel;
}
- (void) commitTransaction
{
[[NSNotificationCenter defaultCenter] postNotificationName:SDStoreTransactionWasCommitted object:self];
transactionLevel--;
if (transactionLevel < 0) transactionLevel = 0;
}
- (unsigned int) nestingLevel
{
return transactionLevel;
}
- (void) freeBlock:(int) blockNumber
{
[rootDictionary removeObjectForKey:[NSString stringWithFormat:@"%d",blockNumber]];
}
///////////////////////////////////////////////////////////////////////////
//
// Utility methods
//
///////////////////////////////////////////////////////////////////////////
- (void) recordLastBlockNumber
{
[privateInfo setObject:[NSString stringWithFormat:@"%d",lastBlock] forKey:BLOCK_NUMBER_KEY];
}
- (int) readLastBlockNumber
{
return [[privateInfo objectForKey:BLOCK_NUMBER_KEY] intValue];
}
- (NSData *)readBlock:(int)blockNumber
{
return [rootDictionary objectForKey: [NSString stringWithFormat:@"%d",blockNumber]];
}
- (void) writeData:(NSData *)blockData toBlock:(int)blockNumber
{
[rootDictionary setObject:blockData forKey:[NSString stringWithFormat:@"%d",blockNumber]];
}
- (void) registerClient:(id)aClient
{
if ([clientList indexOfObjectIdenticalTo:aClient] == NSNotFound)
[clientList addObject:aClient];
}
- (void) unregisterClient:(id)aClient
{
[clientList removeObjectIdenticalTo:aClient];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:nil name:SDStoreTransactionWasCommitted object:self];
[privateInfo release];
[super dealloc];
}
@end
|
|