00001 /** 00002 * @file sync/SyncTreeWorker.h 00003 * 00004 * Part of CCNx Sync. 00005 * 00006 * Copyright (C) 2011 Palo Alto Research Center, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or modify it 00009 * under the terms of the GNU Lesser General Public License version 2.1 00010 * as published by the Free Software Foundation. 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. You should have received 00015 * a copy of the GNU Lesser General Public License along with this library; 00016 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, 00017 * Fifth Floor, Boston, MA 02110-1301 USA. 00018 */ 00019 00020 #ifndef CCN_SyncTreeWorker 00021 #define CCN_SyncTreeWorker 00022 00023 #include "SyncNode.h" 00024 #include "SyncUtil.h" 00025 #include "SyncHashCache.h" 00026 00027 /** 00028 * SyncTreeWorker maintains the state for walking a local sync tree root, 00029 * This is important becase the state cannot be simply kept in a call stack. 00030 */ 00031 00032 enum SyncTreeWorkerState { 00033 SyncTreeWorkerState_init, 00034 SyncTreeWorkerState_needFetch, 00035 SyncTreeWorkerState_fetching, 00036 SyncTreeWorkerState_error 00037 }; 00038 00039 struct SyncTreeWorkerHead { 00040 struct SyncHashCacheHead *cache; 00041 enum SyncTreeWorkerState state; 00042 intmax_t visits; 00043 int remote; 00044 int level; 00045 int lim; 00046 struct SyncTreeWorkerEntry *stack; 00047 }; 00048 00049 struct SyncTreeWorkerEntry { 00050 ssize_t pos; 00051 ssize_t count; 00052 struct SyncHashCacheEntry *cacheEntry; 00053 }; 00054 00055 00056 /** 00057 * create a new tree worker, based on the given cache 00058 * ent != NULL: initialize from the given node 00059 * ent == NULL: create an empty worker, to be externally initialized 00060 * @returns the new worker 00061 */ 00062 struct SyncTreeWorkerHead * 00063 SyncTreeWorkerCreate(struct SyncHashCacheHead *cache, 00064 struct SyncHashCacheEntry *ent, 00065 int remote); 00066 00067 /** 00068 * initialize an existing worker from the cache entry 00069 * resulting level will be 1 00070 */ 00071 void 00072 SyncTreeWorkerInit(struct SyncTreeWorkerHead *head, 00073 struct SyncHashCacheEntry *ent, 00074 int remote); 00075 00076 /** 00077 * requires that the node be present 00078 * @returns the SyncNodeElem from the current position 00079 * @returns NULL if not valid 00080 */ 00081 struct SyncNodeElem * 00082 SyncTreeWorkerGetElem(struct SyncTreeWorkerHead *head); 00083 00084 /** 00085 * @returns the entry at the top of the stack 00086 * @returns NULL if no valid current entry 00087 */ 00088 struct SyncTreeWorkerEntry * 00089 SyncTreeWorkerTop(struct SyncTreeWorkerHead *head); 00090 00091 /** 00092 * pushes into the node at the current position 00093 * @returns the cache entry for the child (if any) 00094 * pushing where there is no node has no effect and returns NULL 00095 */ 00096 struct SyncTreeWorkerEntry * 00097 SyncTreeWorkerPush(struct SyncTreeWorkerHead *head); 00098 00099 /** 00100 * pops the stack and returns the top entry 00101 * popping an empty stack has no effect and returns NULL 00102 */ 00103 struct SyncTreeWorkerEntry * 00104 SyncTreeWorkerPop(struct SyncTreeWorkerHead *head); 00105 00106 /** 00107 * Reset the worker to the given level (or the current level if that is less). 00108 * Resets the position at the new level to 0. 00109 */ 00110 void 00111 SyncTreeWorkerReset(struct SyncTreeWorkerHead *head, int level); 00112 00113 /** 00114 * Free the storage for the worker. 00115 * @returns NULL. 00116 */ 00117 struct SyncTreeWorkerHead * 00118 SyncTreeWorkerFree(struct SyncTreeWorkerHead *head); 00119 00120 /** 00121 * Lookup the name in the tree, starting at the current point, 00122 * with backtrack while the level is greater than the given minimum. 00123 * The lookup can be restarted when a missing node is encountered. 00124 * When SCR_missing is returned, SyncTreeWorkerTop(head) is missing. 00125 */ 00126 enum SyncCompareResult 00127 SyncTreeLookupName(struct SyncTreeWorkerHead *head, 00128 struct ccn_charbuf *name, 00129 int minLevel); 00130 00131 /** 00132 * Generate the names in the tree, starting at the current point, 00133 * with backtrack while the level is greater than the given minimum. 00134 * When SCR_missing is returned, SyncTreeWorkerTop(head) is missing. 00135 */ 00136 enum SyncCompareResult 00137 SyncTreeGenerateNames(struct SyncTreeWorkerHead *head, 00138 struct SyncNameAccum *accum, 00139 int minLevel); 00140 00141 /** 00142 * Mark all reachable cache entries using the current tree worker head, 00143 * with backtrack while the level is greater than the given minimum. 00144 * When SCR_missing is returned, SyncTreeWorkerTop(head) is missing. 00145 * @returns the number of marked nodes. 00146 */ 00147 int 00148 SyncTreeMarkReachable(struct SyncTreeWorkerHead *head, int minLevel); 00149 00150 00151 #endif