/***************************************************************** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. ****************************************************************/ #import "CAYObjectStore.h" @implementation CAYObjectStore -(id)init { self = [super init]; if(self != nil) { diffs = [[NSMutableArray alloc] init]; diffsByObjectId = [[NSMutableDictionary alloc] init]; objectByObjectId = [[NSMutableDictionary alloc] init]; } return self; } -(void)registerDiff:(CAYNodeDiff *)diff { // find diffs for this ObjectId CAYObjectId *oid = (CAYObjectId *)[diff nodeId]; NSMutableArray *diffsForSingleObjectId = (NSMutableArray *)[diffsByObjectId objectForKey:oid]; if(!diffsForSingleObjectId) { diffsForSingleObjectId = [NSMutableArray array]; [diffsByObjectId setObject:diffsForSingleObjectId forKey:oid]; } // check if this diff is the opposite of the prev - if so just remove the prev // instead of adding the new if([diffsForSingleObjectId count] > 0) { CAYNodeDiff *prevDiff = [diffsForSingleObjectId lastObject]; if([diff isOpposite:prevDiff]) { NSLog(@"DEBUG: diff is opposite of prev. remove instead of add. %@", diff); [diffs removeObject:prevDiff]; [diffsForSingleObjectId removeObject:prevDiff]; return; } } // accept this diff NSLog(@"DEBUG: diff is not opposite of prev. add. %@", diff); [diffs addObject:diff]; [diffsForSingleObjectId addObject:diff]; } -(NSUInteger)diffCount { return [diffs count]; } // TODO: should not return a mutable! -(NSMutableArray *)diffs { return diffs; } -(void)removeAllDiffs { [diffs removeAllObjects]; [diffsByObjectId removeAllObjects]; } -(CAYPersistentObject *)objectForId:(CAYObjectId *)oid { return [objectByObjectId objectForKey:oid]; } -(void)setObject:(CAYPersistentObject *)row forId:(CAYObjectId *)oid { [objectByObjectId setObject:row forKey:oid]; } -(void)removeObjectForId:(CAYObjectId *)oid { [objectByObjectId removeObjectForKey:oid]; // TODO: remove from diffs? } -(void)dealloc { [diffs release]; diffs = nil; [diffsByObjectId release]; diffsByObjectId = nil; [objectByObjectId release]; objectByObjectId = nil; [super dealloc]; } @end