00001 /*MT* 00002 00003 MediaTomb - http://www.mediatomb.cc/ 00004 00005 storage_cache.cc - this file is part of MediaTomb. 00006 00007 Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>, 00008 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc> 00009 00010 Copyright (C) 2006-2010 Gena Batyan <bgeradz@mediatomb.cc>, 00011 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>, 00012 Leonhard Wimmer <leo@mediatomb.cc> 00013 00014 MediaTomb is free software; you can redistribute it and/or modify 00015 it under the terms of the GNU General Public License version 2 00016 as published by the Free Software Foundation. 00017 00018 MediaTomb is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 GNU General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 version 2 along with MediaTomb; if not, write to the Free Software 00025 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00026 00027 $Id: storage_cache.cc 2081 2010-03-23 20:18:00Z lww $ 00028 */ 00029 00031 00032 #ifdef HAVE_CONFIG_H 00033 #include "autoconfig.h" 00034 #endif 00035 00036 #include "storage_cache.h" 00037 00038 using namespace zmm; 00039 00040 StorageCache::StorageCache() 00041 { 00042 capacity = STORAGE_CACHE_CAPACITY; 00043 maxfill = STORAGE_CACHE_MAXFILL; 00044 idHash = Ref<DBOHash<int,CacheObject> >(new DBOHash<int,CacheObject>(capacity, INVALID_OBJECT_ID, INVALID_OBJECT_ID_2)); 00045 locationHash = Ref<DSOHash<Array<CacheObject> > >(new DSOHash<Array<CacheObject> >(capacity)); 00046 hasBeenFlushed = false; 00047 mutex = Ref<Mutex> (new Mutex()); 00048 } 00049 00050 void StorageCache::clear() 00051 { 00052 AUTOLOCK(mutex); 00053 idHash->clear(); 00054 locationHash->clear(); 00055 } 00056 00057 Ref<CacheObject> StorageCache::getObject(int id) 00058 { 00059 #ifdef TOMBDEBUG 00060 assert(mutex->isLocked()); 00061 #endif 00062 return idHash->get(id); 00063 } 00064 00065 Ref<CacheObject> StorageCache::getObjectDefinitely(int id) 00066 { 00067 #ifdef TOMBDEBUG 00068 assert(mutex->isLocked()); 00069 #endif 00070 Ref<CacheObject> obj = idHash->get(id); 00071 if (obj == nil) 00072 { 00073 ensureFillLevelOk(); 00074 obj = Ref<CacheObject>(new CacheObject()); 00075 idHash->put(id, obj); 00076 } 00077 return obj; 00078 } 00079 00080 void StorageCache::addChild(int id) 00081 { 00082 #ifdef TOMBDEBUG 00083 assert(mutex->isLocked()); 00084 #endif 00085 Ref<CacheObject> obj = idHash->get(id); 00086 if (obj != nil && obj->knowsNumChildren()) 00087 { 00088 obj->setNumChildren(obj->getNumChildren() + 1); 00089 } 00090 } 00091 00092 bool StorageCache::removeObject(int id) 00093 { 00094 AUTOLOCK(mutex); 00095 Ref<CacheObject> obj = getObject(id); 00096 if (obj == nil) 00097 return false; 00098 if (obj->knowsLocation()) 00099 locationHash->remove(obj->getLocation()); 00100 return idHash->remove(id); 00101 } 00102 00103 Ref<Array<CacheObject> > StorageCache::getObjects(String location) 00104 { 00105 #ifdef TOMBDEBUG 00106 assert(mutex->isLocked()); 00107 #endif 00108 return locationHash->get(location); 00109 } 00110 00111 void StorageCache::checkLocation(Ref<CacheObject> obj) 00112 { 00113 #ifdef TOMBDEBUG 00114 assert(mutex->isLocked()); 00115 #endif 00116 if (! obj->knowsLocation()) 00117 return; 00118 String location = obj->getLocation(); 00119 Ref<Array<CacheObject> > objects = locationHash->get(location); 00120 if (objects == nil) 00121 { 00122 objects = Ref<Array<CacheObject> >(new Array<CacheObject>()); 00123 locationHash->put(location, objects); 00124 } 00125 else 00126 { 00127 for (int i=0;i<objects->size();i++) 00128 { 00129 if (objects->get(i) == obj) 00130 return; 00131 } 00132 } 00133 objects->append(obj); 00134 } 00135 00136 bool StorageCache::flushed() 00137 { 00138 if (! hasBeenFlushed) 00139 return false; 00140 hasBeenFlushed = false; 00141 return true; 00142 } 00143 00144 /* private */ 00145 00146 void StorageCache::ensureFillLevelOk() 00147 { 00148 #ifdef TOMBDEBUG 00149 assert(mutex->isLocked()); 00150 #endif 00151 // TODO - much better... 00152 // for now just clear cache if it gets too full 00153 if (idHash->size() >= maxfill || locationHash->size() >= maxfill) 00154 { 00155 hasBeenFlushed = true; 00156 idHash->clear(); 00157 locationHash->clear(); 00158 } 00159 }
1.6.1