Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added disk_size property to GET response for a database

And added underlying TDDatabase.totalDataSize property.
  • Loading branch information...
commit d88fb60d2a2210b41ea771652539af335b37729d 1 parent ea0a534
@snej snej authored
View
1  Source/TDBlobStore.h
@@ -32,6 +32,7 @@ typedef struct {
@property (readonly) NSUInteger count;
@property (readonly) NSArray* allKeys;
+@property (readonly) UInt64 totalDataSize;
- (NSUInteger) deleteBlobsExceptWithKeys: (NSSet*)keysToKeep;
View
15 Source/TDBlobStore.m
@@ -138,6 +138,21 @@ - (NSUInteger) count {
}
+- (UInt64) totalDataSize {
+ UInt64 total = 0;
+ NSFileManager* fmgr = [NSFileManager defaultManager];
+ for (NSString* filename in [fmgr contentsOfDirectoryAtPath: _path error: nil]) {
+ if ([[self class] getKey: NULL forFilename: filename]) {
+ NSString* itemPath = [_path stringByAppendingPathComponent: filename];
+ NSDictionary* attrs = [fmgr attributesOfItemAtPath: itemPath error: nil];
+ if (attrs)
+ total += attrs.fileSize;
+ }
+ }
+ return total;
+}
+
+
- (NSUInteger) deleteBlobsExceptWithKeys: (NSSet*)keysToKeep {
NSFileManager* fmgr = [NSFileManager defaultManager];
NSArray* blob = [fmgr contentsOfDirectoryAtPath: _path error: nil];
View
1  Source/TDDatabase.h
@@ -80,6 +80,7 @@ extern const TDChangesOptions kDefaultTDChangesOptions;
@property (readonly) NSString* path;
@property (readonly, copy) NSString* name;
@property (readonly) BOOL exists;
+@property (readonly) UInt64 totalDataSize;
/** Begins a database transaction. Transactions can nest. Every -beginTransaction must be balanced by a later -endTransaction:. */
- (BOOL) beginTransaction;
View
8 Source/TDDatabase.m
@@ -211,6 +211,14 @@ - (void) dealloc {
@synthesize path=_path, name=_name, fmdb=_fmdb, attachmentStore=_attachments;
+- (UInt64) totalDataSize {
+ NSDictionary* attrs = [[NSFileManager defaultManager] attributesOfItemAtPath: _path error: nil];
+ if (!attrs)
+ return 0;
+ return attrs.fileSize + _attachments.totalDataSize;
+}
+
+
- (BOOL) beginTransaction {
if (![_fmdb executeUpdate: $sprintf(@"SAVEPOINT tdb%d", _transactionLevel + 1)])
return NO;
View
3  Source/TDRouter+Handlers.m
@@ -157,7 +157,8 @@ - (TDStatus) do_GET: (TDDatabase*)db {
return 500;
_response.bodyObject = $dict({@"db_name", db.name},
{@"doc_count", $object(num_docs)},
- {@"update_seq", $object(update_seq)});
+ {@"update_seq", $object(update_seq)},
+ {@"disk_size", $object(db.totalDataSize)});
return 200;
}
View
17 Source/TDRouter_Tests.m
@@ -87,20 +87,23 @@ static id Send(TDServer* server, NSString* method, NSString* path,
RequireTestCase(TDRouter_Server);
TDServer* server = [TDServer createEmptyAtPath: @"/tmp/TDRouterTest"];
Send(server, @"PUT", @"/database", 201, nil);
- Send(server, @"GET", @"/database", 200,
- $dict({@"db_name", @"database"}, {@"doc_count", $object(0)}, {@"update_seq", $object(0)}));
+
+ NSDictionary* dbInfo = Send(server, @"GET", @"/database", 200, nil);
+ CAssertEq([[dbInfo objectForKey: @"doc_count"] intValue], 0);
+ CAssertEq([[dbInfo objectForKey: @"update_seq"] intValue], 0);
+ CAssert([[dbInfo objectForKey: @"disk_size"] intValue] > 8000);
+
Send(server, @"PUT", @"/database", 412, nil);
Send(server, @"PUT", @"/database2", 201, nil);
Send(server, @"GET", @"/_all_dbs", 200, $array(@"database", @"database2"));
- Send(server, @"GET", @"/database2", 200,
- $dict({@"db_name", @"database2"}, {@"doc_count", $object(0)}, {@"update_seq", $object(0)}));
+ dbInfo = Send(server, @"GET", @"/database2", 200, nil);
+ CAssertEqual([dbInfo objectForKey: @"db_name"], @"database2");
Send(server, @"DELETE", @"/database2", 200, nil);
Send(server, @"GET", @"/_all_dbs", 200, $array(@"database"));
Send(server, @"PUT", @"/database%2Fwith%2Fslashes", 201, nil);
- Send(server, @"GET", @"/database%2Fwith%2Fslashes", 200,
- $dict({@"db_name", @"database/with/slashes"},
- {@"doc_count", $object(0)}, {@"update_seq", $object(0)}));
+ dbInfo = Send(server, @"GET", @"/database%2Fwith%2Fslashes", 200, nil);
+ CAssertEqual([dbInfo objectForKey: @"db_name"], @"database/with/slashes");
}
Please sign in to comment.
Something went wrong with that request. Please try again.