MemoryStore.h

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2005 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #ifndef __MEMORY_STORE_H__
00040 #define __MEMORY_STORE_H__
00041 
00042 #include "config.h"
00043 #include <map>
00044 
00045 #include "../debug/Logger.h"
00046 #include "../thread/SpinLock.h"
00047 #include "../util/ScratchBuffer.h"
00048 #include "../util/StringUtils.h"
00049 
00050 #include "DurableStore.h"
00051 
00052 namespace oasys {
00053 
00054 // forward decls
00055 class MemoryStore;
00056 class MemoryTable;
00057 class MemoryIterator;
00058 class StorageConfig;
00059 
00064 class MemoryTable : public DurableTableImpl, public Logger {
00065     friend class MemoryStore;
00066     friend class MemoryIterator;
00067 
00068 public:
00069     ~MemoryTable();
00070 
00072     int get(const SerializableObject& key,
00073             SerializableObject* data);
00074     
00075     int get(const SerializableObject& key,
00076             SerializableObject** data,
00077             TypeCollection::Allocator_t allocator);
00078     
00079     int put(const SerializableObject& key,
00080             TypeCollection::TypeCode_t typecode,
00081             const SerializableObject* data,
00082             int flags);
00083     
00084     int del(const SerializableObject& key);
00085 
00086     size_t size() const;
00087     
00088     DurableIterator* itr();
00090 
00091 private:
00092     SpinLock lock_;
00093 
00094     struct Item {
00095         ScratchBuffer<u_char*>     key_;
00096         ScratchBuffer<u_char*>     data_;
00097         TypeCollection::TypeCode_t typecode_;
00098     };
00099 
00100     typedef StringMap<Item*>   ItemMap;
00101     ItemMap* items_;
00102     
00103     oasys::ScratchBuffer<u_char*> scratch_;
00104 
00106     MemoryTable(const char* logpath, ItemMap* items,
00107                 const std::string& name, bool multitype);
00108 };
00109 
00116 class MemoryStore : public DurableStoreImpl {
00117     friend class MemoryTable;
00118 
00119 public:
00120     MemoryStore(const char* logpath);
00121 
00122     // Can't copy or =, don't implement these
00123     MemoryStore& operator=(const MemoryStore&);
00124     MemoryStore(const MemoryStore&);
00125 
00126     ~MemoryStore();
00127 
00130     int init(const StorageConfig& cfg);
00131 
00132     int get_table(DurableTableImpl** table,
00133                   const std::string& name,
00134                   int                flags,
00135                   PrototypeVector&   prototypes);
00136 
00137     int del_table(const std::string& name);
00138     int get_table_names(StringVector* names);
00140 
00141 private:
00142     bool init_;        
00143 
00144     typedef StringMap<MemoryTable::ItemMap> TableMap;
00145     TableMap tables_;
00146 };
00147  
00151 class MemoryIterator : public DurableIterator, public Logger {
00152     friend class MemoryTable;
00153 
00154 private:
00159     MemoryIterator(const char* logpath, MemoryTable* t);
00160 
00161 public:
00162     virtual ~MemoryIterator();
00163     
00165     int next();
00166     int get_key(SerializableObject* key);
00168 
00169 protected:
00170     MemoryTable* table_;
00171     bool first_;
00172     MemoryTable::ItemMap::iterator iter_;
00173 };
00174 
00175 }; // namespace oasys
00176 
00177 #endif //__MEMORY_STORE_H__

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1