SyncHashCache.h

Go to the documentation of this file.
00001 /**
00002  * @file sync/SyncHashCache.h
00003  *  
00004  * Part of CCNx Sync.
00005  */
00006 /*
00007  * Copyright (C) 2011-2012 Palo Alto Research Center, Inc.
00008  *
00009  * This library is free software; you can redistribute it and/or modify it
00010  * under the terms of the GNU Lesser General Public License version 2.1
00011  * as published by the Free Software Foundation.
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details. You should have received
00016  * a copy of the GNU Lesser General Public License along with this library;
00017  * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
00018  * Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 #ifndef CCN_SyncHashCache
00022 #define CCN_SyncHashCache
00023 
00024 #include <ccnr/ccnr_private.h>
00025 
00026 struct SyncRootStruct; // defined in SyncRoot
00027 
00028 enum SyncHashState {
00029     SyncHashState_local = 1,     /**< a local node exists */
00030     SyncHashState_remote = 2,    /**< a remote hash has been seen */
00031     SyncHashState_fetching = 4,  /**< remote node is being fetched */
00032     SyncHashState_covered = 8,   /**< remote hash known covered by the local root */
00033     SyncHashState_storing = 16,  /**< local node is queued to be stored */
00034     SyncHashState_stored = 32,   /**< local node has been stored */
00035     SyncHashState_marked = 64    /**< cache entry has been marked */
00036 };
00037 
00038 struct SyncHashCacheHead {
00039     struct SyncRootStruct *root;        /**< the parent root */
00040     uintmax_t probes;                   /**< number of cache probes */
00041     uintmax_t misses;                   /**< number of cache misses */
00042     uintmax_t lastIndex;                /**< assigned by order of creation */
00043     size_t len;                         /**< number of entries */
00044     uint32_t mod;                       /**< the mod to use */
00045     struct SyncHashCacheEntry **ents;   /**< the vector of hash chains */
00046 };
00047 
00048 struct SyncHashCacheEntry {
00049     struct SyncHashCacheHead *head;     /**< the parent head */
00050     struct SyncHashCacheEntry *next;    /**< the next entry in the hash chain */
00051     struct SyncHashCacheEntry *storing; /**< the next entry in the storing chain */
00052     enum SyncHashState state;           /**< state bits */
00053     uintmax_t index;                    /**< assigned by order of creation */
00054     uint32_t busy;                      /**< the tree worker usage count */
00055     uint32_t small;                     /**< the small hash */
00056     struct ccn_charbuf *hash;           /**< hash used to reach this entry */
00057     struct SyncNodeComposite *ncL;      /**< the local node in memory */
00058     struct SyncNodeComposite *ncR;      /**< some remote node in memory */
00059     int64_t lastUsed;                 /**< time when entry last used in compare */
00060     int64_t lastLocalFetch;           /**< time when local entry last fetched */
00061     int64_t lastRemoteFetch;          /**< time when remote entry last fetched */
00062     ccnr_hwm stablePoint;               /**< stable point (roots only) */
00063 };
00064 
00065 /**
00066  * lookup a full hash in a hash table (raw contents, no tag)
00067  * @returns entry if it exists
00068  */
00069 struct SyncHashCacheEntry *
00070 SyncHashLookup(struct SyncHashCacheHead *head,
00071                const unsigned char *xp, ssize_t xs);
00072 
00073 /**
00074  * based on the raw hash, ensure that a remote cache entry exists
00075  * ent->state |= set
00076  */
00077 struct SyncHashCacheEntry *
00078 SyncHashEnter(struct SyncHashCacheHead *head,
00079               const unsigned char *xp, ssize_t xs,
00080               enum SyncHashState set);
00081 
00082 /**
00083  * remove the entry (if present)
00084  */
00085 void
00086 SyncHashRemoveEntry(struct SyncHashCacheHead *head,
00087                     struct SyncHashCacheEntry *ce);
00088 
00089 /**
00090  * clear all marks
00091  */
00092 void
00093 SyncHashClearMarks(struct SyncHashCacheHead *head);
00094 
00095 /**
00096  * create a new hash table with the given modulus (mod == 0 uses a default)
00097  */
00098 struct SyncHashCacheHead *
00099 SyncHashCacheCreate(struct SyncRootStruct *root, uint32_t mod);
00100 
00101 /**
00102  * frees the cache resources
00103  * caller must ensure no further use of the cache
00104  * @returns NULL
00105  */
00106 struct SyncHashCacheHead *
00107 SyncHashCacheFree(struct SyncHashCacheHead *head);
00108 
00109 
00110 /**
00111  * fetches the cache entry
00112  * to be eligible, ce != NULL && ce->ncL != NULL
00113  * && (ce->state & SyncHashState_stored) == 1
00114  * @returns < 0 for failure, 0 if not eligible, and > 0 for success
00115  */
00116 int
00117 SyncCacheEntryFetch(struct SyncHashCacheEntry *ce);
00118 
00119 /**
00120  * stores the cahe entry to the repo
00121  * to be eligible, ce != NULL && ce->ncL == NULL
00122  * && (ce->state & SyncHashState_stored) == 0
00123  * @returns < 0 for failure, 0 if not eligible, and > 0 for success
00124  */
00125 int
00126 SyncCacheEntryStore(struct SyncHashCacheEntry *ce);
00127 
00128 #endif
Generated on Tue Aug 21 14:54:19 2012 for Content-Centric Networking in C by  doxygen 1.6.3