Authored by Michael Tyson

Merge commit '403ee90e' into halset-403ee90e

Conflicts:
	MapView/Map/RMMapContents.m
	MapView/Map/RMPath.m
@@ -48,4 +48,6 @@ @@ -48,4 +48,6 @@
48 -(void) setCapacity: (NSUInteger) theCapacity; 48 -(void) setCapacity: (NSUInteger) theCapacity;
49 -(void) setMinimalPurge: (NSUInteger) thePurgeMinimum; 49 -(void) setMinimalPurge: (NSUInteger) thePurgeMinimum;
50 50
  51 +-(void) purgeTilesFromBefore: (NSDate*) date;
  52 +
51 @end 53 @end
@@ -162,6 +162,14 @@ @@ -162,6 +162,14 @@
162 return image; 162 return image;
163 } 163 }
164 164
  165 +-(void) purgeTilesFromBefore: (NSDate*) date
  166 +{
  167 + @synchronized(self)
  168 + {
  169 + [dao purgeTilesFromBefore:date];
  170 + }
  171 +}
  172 +
165 -(void)didReceiveMemoryWarning 173 -(void)didReceiveMemoryWarning
166 { 174 {
167 @synchronized(self) 175 @synchronized(self)
@@ -41,6 +41,7 @@ @@ -41,6 +41,7 @@
41 -(void) touchTile: (uint64_t) tileHash withDate: (NSDate*) date; 41 -(void) touchTile: (uint64_t) tileHash withDate: (NSDate*) date;
42 -(void) addData: (NSData*) data LastUsed: (NSDate*)date ForTile: (uint64_t) tileHash; 42 -(void) addData: (NSData*) data LastUsed: (NSDate*)date ForTile: (uint64_t) tileHash;
43 -(void) purgeTiles: (NSUInteger) count; 43 -(void) purgeTiles: (NSUInteger) count;
  44 +-(void) purgeTilesFromBefore: (NSDate*) date;
44 -(void) removeAllCachedImages; 45 -(void) removeAllCachedImages;
45 -(void)didReceiveMemoryWarning; 46 -(void)didReceiveMemoryWarning;
46 47
@@ -37,6 +37,9 @@ @@ -37,6 +37,9 @@
37 { 37 {
38 [db executeUpdate:@"CREATE TABLE IF NOT EXISTS ZCACHE (ztileHash INTEGER PRIMARY KEY, zlastUsed DOUBLE, zdata BLOB)"]; 38 [db executeUpdate:@"CREATE TABLE IF NOT EXISTS ZCACHE (ztileHash INTEGER PRIMARY KEY, zlastUsed DOUBLE, zdata BLOB)"];
39 [db executeUpdate:@"CREATE INDEX IF NOT EXISTS zlastUsedIndex ON ZCACHE(zLastUsed)"]; 39 [db executeUpdate:@"CREATE INDEX IF NOT EXISTS zlastUsedIndex ON ZCACHE(zLastUsed)"];
  40 + // adding more than once does not seem to break anything
  41 + [db executeUpdate:@"ALTER TABLE ZCACHE ADD COLUMN zInserted DOUBLE"];
  42 + [db executeUpdate:@"CREATE INDEX IF NOT EXISTS zInsertedIndex ON ZCACHE(zInserted)"];
40 } 43 }
41 44
42 -(id) initWithDatabase: (NSString*)path 45 -(id) initWithDatabase: (NSString*)path
@@ -123,6 +126,27 @@ @@ -123,6 +126,27 @@
123 126
124 } 127 }
125 128
  129 +-(void) purgeTilesFromBefore: (NSDate*) date;
  130 +{
  131 + NSUInteger count = 0;
  132 + FMResultSet *results = [db executeQuery:@"SELECT COUNT(ztileHash) FROM ZCACHE WHERE zInserted < ?", date];
  133 + if ([results next]) {
  134 + count = [results intForColumnIndex:0];
  135 + RMLog(@"Will purge %i tile(s) from before %@", count, date);
  136 + }
  137 + [results close];
  138 +
  139 + if (count == 0) {
  140 + return;
  141 + }
  142 +
  143 + BOOL result = [db executeUpdate: @"DELETE FROM ZCACHE WHERE zInserted < ?",
  144 + date];
  145 + if (result == NO) {
  146 + RMLog(@"Error purging cache");
  147 + }
  148 +}
  149 +
126 -(void) removeAllCachedImages 150 -(void) removeAllCachedImages
127 { 151 {
128 BOOL result = [db executeUpdate: @"DELETE FROM ZCACHE"]; 152 BOOL result = [db executeUpdate: @"DELETE FROM ZCACHE"];
@@ -145,8 +169,8 @@ @@ -145,8 +169,8 @@
145 { 169 {
146 // Fixme 170 // Fixme
147 // RMLog(@"addData\t%d", tileHash); 171 // RMLog(@"addData\t%d", tileHash);
148 - BOOL result = [db executeUpdate:@"INSERT OR IGNORE INTO ZCACHE (ztileHash, zlastUsed, zdata) VALUES (?, ?, ?)",  
149 - [NSNumber numberWithUnsignedLongLong:tileHash], date, data]; 172 + BOOL result = [db executeUpdate:@"INSERT OR REPLACE INTO ZCACHE (ztileHash, zlastUsed, zInserted, zdata) VALUES (?, ?, ?, ?)",
  173 + [NSNumber numberWithUnsignedLongLong:tileHash], date, [NSDate date], data];
150 if (result == NO) 174 if (result == NO)
151 { 175 {
152 RMLog(@"Error occured adding data"); 176 RMLog(@"Error occured adding data");