00001 /** 00002 * @file sync/SyncHashCache.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_SyncHashCache 00021 #define CCN_SyncHashCache 00022 00023 #include "SyncUtil.h" 00024 00025 struct SyncRootStruct; // defined in SyncRoot 00026 00027 enum SyncHashState { 00028 SyncHashState_local = 1, /**< a local node exists */ 00029 SyncHashState_remote = 2, /**< a remote hash has been seen */ 00030 SyncHashState_fetching = 4, /**< remote node is being fetched */ 00031 SyncHashState_covered = 8, /**< remote hash known covered by the local root */ 00032 SyncHashState_storing = 16, /**< local node is queued to be stored */ 00033 SyncHashState_stored = 32, /**< local node has been stored */ 00034 SyncHashState_marked = 64 /**< cache entry has been marked */ 00035 }; 00036 00037 struct SyncHashCacheHead { 00038 struct SyncRootStruct *root; /**< the parent root */ 00039 uintmax_t probes; /**< number of cache probes */ 00040 uintmax_t misses; /**< number of cache misses */ 00041 size_t len; /**< number of entries */ 00042 uint32_t mod; /**< the mod to use */ 00043 struct SyncHashCacheEntry **ents; /**< the vector of hash chains */ 00044 }; 00045 00046 struct SyncHashCacheEntry { 00047 struct SyncHashCacheHead *head; /**< the parent head */ 00048 struct SyncHashCacheEntry *next; /**< the next entry in the hash chain */ 00049 struct SyncHashCacheEntry *storing; /**< the next entry in the storing chain */ 00050 enum SyncHashState state; /**< state bits */ 00051 uint32_t busy; /**< the tree worker usage count */ 00052 uint32_t small; /**< the small hash */ 00053 struct ccn_charbuf *hash; /**< hash used to reach this entry */ 00054 struct SyncNodeComposite *ncL; /**< the local node in memory */ 00055 struct SyncNodeComposite *ncR; /**< some remote node in memory */ 00056 sync_time lastUsed; /**< time when entry last used in compare */ 00057 sync_time lastLocalFetch; /**< time when local entry last fetched */ 00058 sync_time lastRemoteFetch; /**< time when remote entry last fetched */ 00059 ccnr_hwm stablePoint; /**< stable point (roots only) */ 00060 }; 00061 00062 /** 00063 * lookup a full hash in a hash table (raw contents, no tag) 00064 * @returns entry if it exists 00065 */ 00066 struct SyncHashCacheEntry * 00067 SyncHashLookup(struct SyncHashCacheHead *head, 00068 const unsigned char *xp, ssize_t xs); 00069 00070 /** 00071 * based on the raw hash, ensure that a remote cache entry exists 00072 * ent->state |= set 00073 */ 00074 struct SyncHashCacheEntry * 00075 SyncHashEnter(struct SyncHashCacheHead *head, 00076 const unsigned char *xp, ssize_t xs, 00077 enum SyncHashState set); 00078 00079 /** 00080 * remove the entry (if present) 00081 */ 00082 void 00083 SyncHashRemoveEntry(struct SyncHashCacheHead *head, 00084 struct SyncHashCacheEntry *ce); 00085 00086 /** 00087 * clear all marks 00088 */ 00089 void 00090 SyncHashClearMarks(struct SyncHashCacheHead *head); 00091 00092 /** 00093 * create a new hash table with the given modulus (mod == 0 uses a default) 00094 */ 00095 struct SyncHashCacheHead * 00096 SyncHashCacheCreate(struct SyncRootStruct *root, uint32_t mod); 00097 00098 /** 00099 * frees the cache resources 00100 * caller must ensure no further use of the cache 00101 * @returns NULL 00102 */ 00103 struct SyncHashCacheHead * 00104 SyncHashCacheFree(struct SyncHashCacheHead *head); 00105 00106 00107 /** 00108 * fetches the cache entry 00109 * to be eligible, ce != NULL && ce->ncL != NULL 00110 * && (ce->state & SyncHashState_stored) == 1 00111 * @returns < 0 for failure, 0 if not eligible, and > 0 for success 00112 */ 00113 int 00114 SyncCacheEntryFetch(struct SyncHashCacheEntry *ce); 00115 00116 /** 00117 * stores the cahe entry to the repo 00118 * to be eligible, ce != NULL && ce->ncL == NULL 00119 * && (ce->state & SyncHashState_stored) == 0 00120 * @returns < 0 for failure, 0 if not eligible, and > 0 for success 00121 */ 00122 int 00123 SyncCacheEntryStore(struct SyncHashCacheEntry *ce); 00124 00125 #endif