Skip to content

Commit

Permalink
Before this changeset findAll and ARBase looked up
Browse files Browse the repository at this point in the history
and cached the column names each time you accessed
a row.  So find 100 rows and access a column in each
one and you have looked up the columns from the
db 100 times and cached them in 100 caches.

With this changeset the column name caching moves into
the connection, so no matter how many models you
have, the table columns will only be cached once.
  • Loading branch information
face committed Jan 26, 2009
1 parent f38b07d commit 0c39077
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
1 change: 0 additions & 1 deletion Source/ARBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ typedef enum {
NSMutableDictionary *writeCache;
NSMutableArray *addCache;
NSMutableArray *removeCache;
NSArray *columnCache;
}
@property(readwrite, retain) id<ARConnection> connection;
@property(readwrite, retain) NSMutableArray *relationships;
Expand Down
6 changes: 1 addition & 5 deletions Source/ARBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,7 @@ - (void)removeRecord:(id)record forKey:(NSString *)key ignoreCache:(BOOL)ignoreC
#pragma mark Database interface
- (NSArray *)columns
{
if(!columnCache)
columnCache = [[self.connection columnsForTable:[[self class] tableName]] retain];
return columnCache;
return [[self.connection columnsForTable:[[self class] tableName]] retain];
}
+ (NSString *)idColumnForModel:(Class)modelClass
{
Expand Down Expand Up @@ -422,8 +420,6 @@ - (void)dealloc
[readCache release];
[addCache release];
[removeCache release];
if(columnCache)
[columnCache release];

[super dealloc];
}
Expand Down
14 changes: 14 additions & 0 deletions Source/Connections/ARMySQLConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/*! @cond IGNORE */
@interface ARMySQLConnection ()
NSMutableDictionary *columnCache;
- (NSString *)lastErrorMessage;
- (BOOL)ping;
- (BOOL)connectWithHost:(NSString *)host
Expand Down Expand Up @@ -143,6 +144,16 @@ - (MYSQL *)mySQLConnection
}
- (NSArray *)columnsForTable:(NSString *)tableName
{
NSMutableArray *fieldNames = nil;

if (!columnCache)
columnCache = [NSMutableDictionary dictionary];
else
fieldNames = [columnCache objectForKey:tableName];

if ( fieldNames )
return fieldNames;

MYSQL_RES *result = mysql_list_fields(mySQLConnection, [tableName UTF8String], "%");
if(result)
{
Expand All @@ -152,6 +163,7 @@ - (NSArray *)columnsForTable:(NSString *)tableName
{
[fieldNames addObject:field.name];
}
[columnCache setObject:fieldNames forKey:tableName];
return fieldNames;
}
else
Expand Down Expand Up @@ -258,6 +270,8 @@ - (void)finalize
- (void)dealloc
{
[self closeConnection];
if(columnCache)
[columnCache release];
[super dealloc];
}
@end
2 changes: 2 additions & 0 deletions Source/Connections/ARSQLiteConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ typedef enum {

@interface ARSQLiteConnection : NSObject <ARConnection> {
sqlite3 *database;
NSDateFormatter *dateFormatter;
NSMutableDictionary *columnCache;
}
/*!
* Returns a ready to use sqlite "connection"\n
Expand Down
16 changes: 14 additions & 2 deletions Source/Connections/ARSQLiteConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

/*! @cond IGNORE */
@interface ARSQLiteConnection ()
NSDateFormatter *dateFormatter = nil;
- (sqlite3_stmt *)prepareQuerySQL:(NSString *)query;
- (void)finalizeQuery:(sqlite3_stmt *)query;
- (NSArray *)columnsForQuery:(sqlite3_stmt *)query;
Expand Down Expand Up @@ -149,9 +148,22 @@ - (NSUInteger)lastInsertId

- (NSArray *)columnsForTable:(NSString *)tableName
{

NSArray *columns = nil;

if (!columnCache)
columnCache = [[NSMutableDictionary dictionary] retain];
else
columns = [columnCache objectForKey:tableName];

if ( columns )
return columns;

sqlite3_stmt *queryByteCode = [self prepareQuerySQL:[NSString stringWithFormat:@"SELECT * FROM %@", tableName]];
NSArray *columns = [self columnsForQuery:queryByteCode];
columns = [self columnsForQuery:queryByteCode];
[self finalizeQuery:queryByteCode];

[columnCache setObject:columns forKey:tableName];
return columns;
}

Expand Down

0 comments on commit 0c39077

Please sign in to comment.