DynamicParam.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/exec/DynamicParam.cpp#12 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2004-2009 SQLstream, Inc.
00006 // Copyright (C) 2004-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 #include "fennel/common/CommonPreamble.h"
00025 #include "fennel/exec/DynamicParam.h"
00026 #include "fennel/tuple/TupleAccessor.h"
00027 #include "fennel/tuple/TupleData.h"
00028 #include "fennel/tuple/StandardTypeDescriptor.h"
00029 
00030 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/exec/DynamicParam.cpp#12 $");
00031 
00032 DynamicParam::DynamicParam(
00033     TupleAttributeDescriptor const &descInit,
00034     bool isCounterInit)
00035     : desc(descInit) , isCounter(isCounterInit)
00036 {
00037     pBuffer.reset(new FixedBuffer[desc.cbStorage]);
00038 }
00039 
00040 void DynamicParamManager::createParam(
00041     DynamicParamId dynamicParamId,
00042     const TupleAttributeDescriptor &attrDesc,
00043     bool failIfExists)
00044 {
00045     StrictMutexGuard mutexGuard(mutex);
00046 
00047     SharedDynamicParam param(new DynamicParam(attrDesc));
00048     createParam(dynamicParamId, param, failIfExists);
00049 }
00050 
00051 void DynamicParamManager::createParam(
00052     DynamicParamId dynamicParamId,
00053     SharedDynamicParam param,
00054     bool failIfExists)
00055 {
00056     ParamMapConstIter pExisting = paramMap.find(dynamicParamId);
00057     if (pExisting != paramMap.end()) {
00058         permAssert(!failIfExists);
00059         assert(param->desc == pExisting->second->getDesc());
00060         return;
00061     }
00062     paramMap.insert(ParamMap::value_type(dynamicParamId, param));
00063 }
00064 
00065 void DynamicParamManager::createCounterParam(
00066     DynamicParamId dynamicParamId,
00067     bool failIfExists)
00068 {
00069     StrictMutexGuard mutexGuard(mutex);
00070 
00071     StandardTypeDescriptorFactory stdTypeFactory;
00072     TupleAttributeDescriptor attrDesc(
00073         stdTypeFactory.newDataType(STANDARD_TYPE_INT_64));
00074     SharedDynamicParam param(new DynamicParam(attrDesc, true));
00075     createParam(dynamicParamId, param, failIfExists);
00076 
00077     // initialize the parameter to zero
00078     PBuffer buf = param->pBuffer.get();
00079     int64_t *pCounter = reinterpret_cast<int64_t *>(buf);
00080     *pCounter = 0;
00081     param->datum.pData = buf;
00082     param->datum.cbData = sizeof(int64_t);
00083 }
00084 
00085 void DynamicParamManager::deleteParam(DynamicParamId dynamicParamId)
00086 {
00087     StrictMutexGuard mutexGuard(mutex);
00088 
00089     assert(paramMap.find(dynamicParamId) != paramMap.end());
00090     paramMap.erase(dynamicParamId);
00091     assert(paramMap.find(dynamicParamId) == paramMap.end());
00092 }
00093 
00094 void DynamicParamManager::writeParam(
00095     DynamicParamId dynamicParamId, const TupleDatum &src)
00096 {
00097     StrictMutexGuard mutexGuard(mutex);
00098 
00099     DynamicParam &param = getParamInternal(dynamicParamId);
00100     if (src.pData) {
00101         assert(src.cbData <= param.getDesc().cbStorage);
00102     }
00103     param.datum.pData = param.pBuffer.get();
00104     param.datum.memCopyFrom(src);
00105 }
00106 
00107 DynamicParam const &DynamicParamManager::getParam(
00108     DynamicParamId dynamicParamId)
00109 {
00110     StrictMutexGuard mutexGuard(mutex);
00111     return getParamInternal(dynamicParamId);
00112 }
00113 
00114 DynamicParam &DynamicParamManager::getParamInternal(
00115     DynamicParamId dynamicParamId)
00116 {
00117     ParamMapConstIter pExisting = paramMap.find(dynamicParamId);
00118     assert(pExisting != paramMap.end());
00119     return *(pExisting->second.get());
00120 }
00121 
00122 void DynamicParamManager::readParam(
00123     DynamicParamId dynamicParamId, TupleDatum &dest)
00124 {
00125     StrictMutexGuard mutexGuard(mutex);
00126     dest.memCopyFrom(getParamInternal(dynamicParamId).datum);
00127 }
00128 
00129 void DynamicParamManager::incrementCounterParam(DynamicParamId dynamicParamId)
00130 {
00131     StrictMutexGuard mutexGuard(mutex);
00132 
00133     DynamicParam &param = getParamInternal(dynamicParamId);
00134     assert(param.isCounter);
00135     int64_t *pCounter = reinterpret_cast<int64_t *>(param.pBuffer.get());
00136     (*pCounter)++;
00137 }
00138 
00139 void DynamicParamManager::decrementCounterParam(DynamicParamId dynamicParamId)
00140 {
00141     StrictMutexGuard mutexGuard(mutex);
00142 
00143     DynamicParam &param = getParamInternal(dynamicParamId);
00144     assert(param.isCounter);
00145     int64_t *pCounter = reinterpret_cast<int64_t *>(param.pBuffer.get());
00146     (*pCounter)--;
00147 }
00148 
00149 void DynamicParamManager::deleteAllParams()
00150 {
00151     StrictMutexGuard mutexGuard(mutex);
00152     paramMap.erase(paramMap.begin(), paramMap.end());
00153 }
00154 
00155 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/exec/DynamicParam.cpp#12 $");
00156 
00157 // End DynamicParam.cpp

Generated on Mon Jun 22 04:00:18 2009 for Fennel by  doxygen 1.5.1