Skip to content

Commit

Permalink
Merge pull request #16 from Tightdb/ks-descriptor
Browse files Browse the repository at this point in the history
Now using new Descriptor based API (Spec is no longer a public class)
  • Loading branch information
kspangsege committed Feb 4, 2014
2 parents f92d036 + 55e5731 commit 85b209e
Show file tree
Hide file tree
Showing 19 changed files with 2,956 additions and 2,740 deletions.
163 changes: 82 additions & 81 deletions src/tightdb/objc/group_objc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,109 +15,103 @@
using namespace std;


@interface TightdbGroup()
@property(nonatomic) tightdb::Group *group;
@property(nonatomic) BOOL readOnly;
@end

@implementation TightdbGroup
@synthesize group = _group;
@synthesize readOnly = _readOnly;
{
tightdb::Group* m_group;
BOOL m_is_owned;
BOOL m_read_only;
}

+(TightdbGroup *)group
+(TightdbGroup*)group
{
return [self groupWithError:nil];
}

+(TightdbGroup *)groupWithError:(NSError *__autoreleasing *)error
+(TightdbGroup*)groupWithError:(NSError* __autoreleasing*)error
{
TightdbGroup *group = [[TightdbGroup alloc] init];
TIGHTDB_EXCEPTION_ERRHANDLER(
group.group = new tightdb::Group();
, @"com.tightdb.group", nil);
group.readOnly = NO;
TightdbGroup* group = [[TightdbGroup alloc] init];
TIGHTDB_EXCEPTION_ERRHANDLER(group->m_group = new tightdb::Group;, nil);
group->m_is_owned = YES;
group->m_read_only = NO;
return group;
}

// Careful with this one - Remember that group will be deleted on dealloc.
+(TightdbGroup *)groupTightdbGroup:(tightdb::Group *)tightdbGroup readOnly:(BOOL)readOnly
+(TightdbGroup*)groupWithNativeGroup:(tightdb::Group*)group isOwned:(BOOL)is_owned readOnly:(BOOL)read_only
{
TightdbGroup *group = [[TightdbGroup alloc] init];
group.group = tightdbGroup;
group.readOnly = readOnly;
return group;
TightdbGroup* group_2 = [[TightdbGroup alloc] init];
group_2->m_group = group;
group_2->m_is_owned = is_owned;
group_2->m_read_only = read_only;
return group_2;
}


+(TightdbGroup *)groupWithFilename:(NSString *)filename
+(TightdbGroup*)groupWithFilename:(NSString*)filename
{
return [self groupWithFilename:filename error:nil];
}

+(TightdbGroup *)groupWithFilename:(NSString *)filename error:(NSError **)error
+(TightdbGroup*)groupWithFilename:(NSString*)filename error:(NSError**)error
{
tightdb::Group* group;
TightdbGroup* group = [[TightdbGroup alloc] init];
if (!group)
return nil;
TIGHTDB_EXCEPTION_ERRHANDLER(
group = new tightdb::Group(tightdb::StringData(ObjcStringAccessor(filename)));
, @"com.tightdb.group", nil);
TightdbGroup* group2 = [[TightdbGroup alloc] init];
if (group2) {
group2.group = group;
group2.readOnly = NO;
}
return group2;
group->m_group = new tightdb::Group(tightdb::StringData(ObjcStringAccessor(filename)));,
nil);
group->m_is_owned = YES;
group->m_read_only = NO;
return group;
}

+(TightdbGroup *)groupWithBuffer:(const char *)data size:(size_t)size
+(TightdbGroup*)groupWithBuffer:(const char*)data size:(size_t)size
{
return [self groupWithBuffer:data size:size error:nil];
}

+(TightdbGroup *)groupWithBuffer:(const char *)data size:(size_t)size error:(NSError *__autoreleasing *)error
+(TightdbGroup*)groupWithBuffer:(const char*)data size:(size_t)size error:(NSError* __autoreleasing*)error
{
tightdb::Group* group;
TightdbGroup* group = [[TightdbGroup alloc] init];
if (!group)
return nil;
TIGHTDB_EXCEPTION_ERRHANDLER(
group = new tightdb::Group(tightdb::BinaryData(data, size));
, @"com.tightdb.group", nil);
TightdbGroup* group2 = [[TightdbGroup alloc] init];
group2.group = group;
group2.readOnly = NO;
return group2;
}

-(void)clearGroup
{
_group = 0;
group->m_group = new tightdb::Group(tightdb::BinaryData(data, size));,
nil);
group->m_is_owned = YES;
group->m_read_only = NO;
return group;
}

-(void)dealloc
{
#ifdef TIGHTDB_DEBUG
NSLog(@"TightdbGroup dealloc");
#endif
delete _group;
if (m_is_owned)
delete m_group;
}


-(size_t)getTableCount
{
return _group->size();
return m_group->size();
}
-(NSString *)getTableName:(size_t)table_ndx
-(NSString*)getTableName:(size_t)table_ndx
{
return to_objc_string(_group->get_table_name(table_ndx));
return to_objc_string(m_group->get_table_name(table_ndx));
}

-(BOOL)write:(NSString *)filePath
-(BOOL)write:(NSString*)file_path
{
return [self write:filePath error:nil];
return [self write:file_path error:nil];
}

-(BOOL)write:(NSString *)filePath error:(NSError *__autoreleasing *)error
-(BOOL)write:(NSString*)file_path error:(NSError* __autoreleasing*)error
{
TIGHTDB_EXCEPTION_ERRHANDLER(
_group->write(tightdb::StringData(ObjcStringAccessor(filePath)));
, @"com.tightdb.group", NO);
m_group->write(tightdb::StringData(ObjcStringAccessor(file_path)));,
NO);
return YES;
}

Expand All @@ -126,67 +120,74 @@ -(const char*)writeToMem:(size_t*)size
return [self writeToMem:size error:nil];
}

-(const char*)writeToMem:(size_t*)size error:(NSError *__autoreleasing *)error
-(const char*)writeToMem:(size_t*)size error:(NSError* __autoreleasing*)error
{
TIGHTDB_EXCEPTION_ERRHANDLER(
tightdb::BinaryData buffer = _group->write_to_mem();
*size = buffer.size();
return buffer.data();
, @"com.tightdb.group", nil);
tightdb::BinaryData buffer = m_group->write_to_mem();
*size = buffer.size();
return buffer.data();,
nil);
}

-(BOOL)hasTable:(NSString *)name
-(BOOL)hasTable:(NSString*)name
{
return _group->has_table(ObjcStringAccessor(name));
return m_group->has_table(ObjcStringAccessor(name));
}

// FIXME: Avoid creating a table instance. It should be enough to create an TightdbSpec and then check that.
// FIXME: Avoid creating a table instance. It should be enough to create an TightdbDescriptor and then check that.
// FIXME: Check that the specified class derives from Table.
// FIXME: Find a way to avoid having to transcode the table name twice
-(BOOL)hasTable:(NSString *)name withClass:(__unsafe_unretained Class)classObj
-(BOOL)hasTable:(NSString*)name withClass:(__unsafe_unretained Class)class_obj
{
if (!_group->has_table(ObjcStringAccessor(name))) return NO;
TightdbTable* table = [self getTable:name withClass:classObj];
if (!m_group->has_table(ObjcStringAccessor(name)))
return NO;
TightdbTable* table = [self getTable:name withClass:class_obj];
return table != nil;
}

-(id)getTable:(NSString *)name
-(id)getTable:(NSString*)name
{
return [self getTable:name error:nil];
}

-(id)getTable:(NSString *)name error:(NSError *__autoreleasing *)error
-(id)getTable:(NSString*)name error:(NSError* __autoreleasing*)error
{
TightdbTable *table = [[TightdbTable alloc] _initRaw];
if (TIGHTDB_UNLIKELY(!table)) return nil;
TightdbTable* table = [[TightdbTable alloc] _initRaw];
if (TIGHTDB_UNLIKELY(!table))
return nil;
TIGHTDB_EXCEPTION_ERRHANDLER(
[table setTable:_group->get_table(ObjcStringAccessor(name))];
, @"com.tightdb.group", nil);
tightdb::TableRef table_2 = m_group->get_table(ObjcStringAccessor(name));
[table setNativeTable:table_2.get()];,
nil);
[table setParent:self];
[table setReadOnly:_readOnly];
[table setReadOnly:m_read_only];
return table;
}

-(id)getTable:(NSString *)name withClass:(__unsafe_unretained Class)classObj
-(id)getTable:(NSString*)name withClass:(__unsafe_unretained Class)class_obj
{
return [self getTable:name withClass:classObj error:nil];
return [self getTable:name withClass:class_obj error:nil];
}
// FIXME: Check that the specified class derives from Table.
-(id)getTable:(NSString *)name withClass:(__unsafe_unretained Class)classObj error:(NSError *__autoreleasing *)error
-(id)getTable:(NSString*)name withClass:(__unsafe_unretained Class)class_obj error:(NSError* __autoreleasing*)error
{
TightdbTable *table = [[classObj alloc] _initRaw];
if (TIGHTDB_UNLIKELY(!table)) return nil;
TightdbTable* table = [[class_obj alloc] _initRaw];
if (TIGHTDB_UNLIKELY(!table))
return nil;
bool was_created;
TIGHTDB_EXCEPTION_ERRHANDLER(
[table setTable:_group->get_table(ObjcStringAccessor(name), was_created)];
, @"com.tightdb.group", nil);
tightdb::TableRef table_2 = m_group->get_table(ObjcStringAccessor(name), was_created);
[table setNativeTable:table_2.get()];,
nil);
[table setParent:self];
[table setReadOnly:_readOnly];
[table setReadOnly:m_read_only];
if (was_created) {
if (![table _addColumns]) return nil;
if (![table _addColumns])
return nil;
}
else {
if (![table _checkType]) return nil;
if (![table _checkType])
return nil;
}
return table;
}
Expand Down
5 changes: 3 additions & 2 deletions src/tightdb/objc/group_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


@interface TightdbGroup()
+(TightdbGroup *)groupTightdbGroup:(tightdb::Group *)tightdbGroup readOnly:(BOOL)readOnly;
-(void)clearGroup;

+(TightdbGroup*)groupWithNativeGroup:(tightdb::Group*)group isOwned:(BOOL)is_owned readOnly:(BOOL)read_only;

@end
53 changes: 26 additions & 27 deletions src/tightdb/objc/group_shared_objc.mm
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <tightdb/util/unique_ptr.hpp>
#include <tightdb/group_shared.hpp>

#import <tightdb/objc/group_shared.h>
Expand All @@ -10,14 +11,16 @@

@implementation TightdbSharedGroup
{
tightdb::SharedGroup* _sharedGroup;
tightdb::util::UniquePtr<tightdb::SharedGroup> m_shared_group;
}

+(TightdbSharedGroup *)groupWithFilename:(NSString *)filename
+(TightdbSharedGroup*)groupWithFilename:(NSString*)filename
{
tightdb::SharedGroup* shared_group;
TightdbSharedGroup* shared_group = [[TightdbSharedGroup alloc] init];
if (!shared_group)
return nil;
try {
shared_group = new tightdb::SharedGroup(tightdb::StringData(ObjcStringAccessor(filename)));
shared_group->m_shared_group.reset(new tightdb::SharedGroup(tightdb::StringData(ObjcStringAccessor(filename))));
}
catch (...) {
// FIXME: Diffrent exception types mean different things. More
Expand All @@ -30,50 +33,46 @@ +(TightdbSharedGroup *)groupWithFilename:(NSString *)filename
// anything derived from std::exception.
return nil;
}
TightdbSharedGroup* shared_group2 = [[TightdbSharedGroup alloc] init];
if (shared_group2) {
shared_group2->_sharedGroup = shared_group;
}
return shared_group2;
return shared_group;
}

-(void)dealloc
{
delete _sharedGroup;
_sharedGroup = 0;
#ifdef TIGHTDB_DEBUG
NSLog(@"TightdbSharedGroup dealloc");
#endif
}


-(void)readTransaction:(TightdbSharedGroupReadTransactionBlock)block
{
TightdbGroup* group;
@try {
group = [TightdbGroup groupTightdbGroup:(tightdb::Group *)&_sharedGroup->begin_read() readOnly:YES];
block(group);
const tightdb::Group& group = m_shared_group->begin_read();
TightdbGroup* group_2 = [TightdbGroup groupWithNativeGroup:const_cast<tightdb::Group*>(&group) isOwned:NO readOnly:YES];
block(group_2);
}
@catch (NSException *exception) {
@catch (NSException* exception) {
@throw exception;
}
@finally {
_sharedGroup->end_read();
[group clearGroup];
m_shared_group->end_read();
}
}

-(void)writeTransaction:(TightdbSharedGroupWriteTransactionBlock)block
{
TightdbGroup* group;
@try {
group = [TightdbGroup groupTightdbGroup:&_sharedGroup->begin_write() readOnly:NO];
if (block(group))
_sharedGroup->commit();
else
_sharedGroup->rollback();
[group clearGroup];
tightdb::Group& group = m_shared_group->begin_write();
TightdbGroup* group_2 = [TightdbGroup groupWithNativeGroup:&group isOwned:NO readOnly:NO];
if (block(group_2)) {
m_shared_group->commit();
}
else {
m_shared_group->rollback();
}
}
@catch (NSException *exception) {
_sharedGroup->rollback();
[group clearGroup];
@catch (NSException* exception) {
m_shared_group->rollback();
@throw exception;
}
}
Expand Down
Loading

0 comments on commit 85b209e

Please sign in to comment.