OW_AssocDb.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #ifndef OW_ASSOCDB_HPP_INCLUDE_GUARD_
00037 #define OW_ASSOCDB_HPP_INCLUDE_GUARD_
00038 #include "OW_config.h"
00039 #include "OW_Types.hpp"
00040 #include "OW_Index.hpp"
00041 #include "OW_Array.hpp"
00042 #include "OW_String.hpp"
00043 #include "OW_File.hpp"
00044 #include "OW_MutexLock.hpp"
00045 #include "OW_CIMFwd.hpp"
00046 #include "OW_ServiceEnvironmentIFC.hpp"
00047 #include "OW_SortedVectorSet.hpp"
00048 #include "OW_CIMObjectPath.hpp"
00049 #include "OW_ResultHandlerIFC.hpp"
00050 #include "OW_IntrusiveReference.hpp"
00051 #include "OW_IntrusiveCountableBase.hpp"
00052 
00053 namespace OW_NAMESPACE
00054 {
00055 
00059 struct OW_HDB_API AssocDbEntry
00060 {
00061    AssocDbEntry(std::istream& istrm);
00062    AssocDbEntry()
00063       : m_objectName(CIMNULL)
00064       , m_offset(-1L)
00065    {}
00066 
00067    AssocDbEntry(const CIMObjectPath& objectName,
00068       const CIMName& role,
00069       const CIMName& resultRole);
00070 
00071    struct entry
00072    {
00073       entry ()
00074          : m_associatedObject(CIMNULL)
00075          , m_associationPath(CIMNULL)
00076       {}
00077       CIMName m_assocClass;
00078       CIMName m_resultClass;
00079       CIMObjectPath m_associatedObject; // value for associtor(Name)s
00080       CIMObjectPath m_associationPath;  // value for reference(Name)s
00081       
00082       void writeObject(std::ostream& ostrm) const;
00083       void readObject(std::istream& istrm);
00084    };
00085 
00086    void writeObject(std::ostream& ostrm) const;
00087    void readObject(std::istream& istrm);
00088    Int32 getOffset() const { return m_offset; }
00089    void setOffset(Int32 offset) { m_offset = offset; }
00090 
00091    static String makeKey(const CIMObjectPath& objectName, const CIMName& role,
00092       const CIMName& resultRole);
00093    
00094    String makeKey() const;
00095 
00096    typedef CIMObjectPath AssocDbEntry::*safe_bool;
00097    operator safe_bool () const
00098       {  return m_objectName ? &AssocDbEntry::m_objectName : 0; }
00099    bool operator!() const
00100       {  return !m_objectName; }
00101 
00102 #ifdef OW_WIN32
00103 #pragma warning (push)
00104 #pragma warning (disable: 4251)
00105 #endif
00106 
00107    CIMObjectPath m_objectName; // part 1 of key
00108    CIMName m_role; // part 2 of key
00109    CIMName m_resultRole; // part 3 of key
00110    Array<entry> m_entries;
00111    Int32 m_offset;
00112 
00113 #ifdef OW_WIN32
00114 #pragma warning (pop)
00115 #endif
00116 
00117 };
00118 OW_HDB_API std::ostream& operator << (std::ostream& ostrm, const AssocDbEntry& arg);
00119 typedef Array<AssocDbEntry> AssocDbEntryArray;
00120 OW_HDB_API bool operator==(const AssocDbEntry::entry& lhs, const AssocDbEntry::entry& rhs);
00122 class AssocDb;
00124 typedef ResultHandlerIFC<AssocDbEntry::entry> AssocDbEntryResultHandlerIFC;
00125 class OW_HDB_API AssocDbHandle
00126 {
00127 private:
00128    struct AssocDbHandleData : public IntrusiveCountableBase
00129    {
00130       AssocDbHandleData();
00131       AssocDbHandleData(const AssocDbHandleData& arg);
00132       AssocDbHandleData(AssocDb* pdb, const File& file);
00133       ~AssocDbHandleData();
00134       AssocDbHandleData& operator= (const AssocDbHandleData& arg);
00135       AssocDb* m_pdb;
00136       File m_file;
00137    };
00138    typedef IntrusiveReference<AssocDbHandleData> AssocDbHandleDataRef;
00139 
00140 public:
00141    AssocDbHandle() : m_pdata(NULL) {}
00142    AssocDbHandle(const AssocDbHandle& arg) : m_pdata(arg.m_pdata) {}
00143    AssocDbHandle& operator= (const AssocDbHandle& arg)
00144    {
00145       m_pdata = arg.m_pdata;
00146       return *this;
00147    }
00152    bool hasAssocEntries(const String& ns, const CIMObjectPath& instanceName);
00157    //void addEntry(const AssocDbEntry& newEntry);
00158    void addEntry(const CIMObjectPath& objectName,
00159       const CIMName& assocClassName, const CIMName& resultClass,
00160       const CIMName& role, const CIMName& resultRole,
00161       const CIMObjectPath& associatedObject,
00162       const CIMObjectPath& assocClassPath);
00170    void addEntries(const String& ns, const CIMInstance& assocInstance);
00171    void addEntries(const String& ns, const CIMClass& assocClass);
00176    //void deleteEntry(const AssocDbEntry& entryToDelete);
00177    void deleteEntry(const CIMObjectPath& objectName,
00178       const CIMName& assocClassName, const CIMName& resultClass,
00179       const CIMName& role, const CIMName& resultRole,
00180       const CIMObjectPath& associatedObject,
00181       const CIMObjectPath& assocClassPath);
00186    //void deleteEntries(const AssocDbEntryArray& entryra);
00194    void deleteEntries(const String& ns, const CIMInstance& assocInstance);
00195    void deleteEntries(const String& ns, const CIMClass& assocClass);
00203    //void deleteEntries(const String& objectName);
00213    void getAllEntries(const CIMObjectPath& objectName,
00214       const SortedVectorSet<CIMName>* passocClasses,
00215       const SortedVectorSet<CIMName>* presultClasses,
00216       const CIMName& role,
00217       const CIMName& resultRole,
00218       AssocDbEntryResultHandlerIFC& result);
00219    File getFile() const { return m_pdata->m_file; }
00220 
00221    typedef AssocDbHandleDataRef AssocDbHandle::*safe_bool;
00222    operator safe_bool () const
00223       {  return m_pdata ? &AssocDbHandle::m_pdata : 0; }
00224    bool operator!() const
00225       {  return !m_pdata; }
00226 private:
00227    
00228    void addOrDeleteEntries(const String& ns, const CIMInstance& assocInstance, bool add);
00229    void addOrDeleteEntries(const String& ns, const CIMClass& assocClass, bool add);
00230 
00231 #ifdef OW_WIN32
00232 #pragma warning (push)
00233 #pragma warning (disable: 4251)
00234 #endif
00235 
00236    AssocDbHandle(AssocDb* pdb, const File& file) :
00237       m_pdata(new AssocDbHandleData(pdb, file)) {}
00238    AssocDbHandleDataRef m_pdata;
00239    friend class AssocDb;
00240 
00241 #ifdef OW_WIN32
00242 #pragma warning (pop)
00243 #endif
00244 
00245 };
00246 // The following structure represents the format of header that
00247 // preceeds all records in the associations db
00248 // TODO: Fix all the code that uses this thing that assumes it's all packed data.
00249 struct OW_HDB_API AssocDbRecHeader
00250 {
00251    AssocDbRecHeader() { memset(this, 0, sizeof(*this)); }
00252    UInt32 chkSum;
00253    Int32 nextFree;
00254    UInt32 blkSize;
00255    UInt32 flags;
00256    size_t dataSize;
00257 };
00258 #define OW_ASSOCSIGNATURE "OWASSOCIATORFI2"
00259 #define OW_ASSOCSIGLEN 16
00260 // The following structure represents the file header for the
00261 // associations database
00262 struct OW_HDB_API AssocDbHeader
00263 {
00264    char signature[OW_ASSOCSIGLEN];
00265    Int32 firstFree;
00266    UInt32 version;
00267 };
00268 
00269 class OW_HDB_API AssocDb
00270 {
00271 public:
00272    AssocDb();
00273    ~AssocDb();
00279    void open(const String& fileName);
00280    void init(const ServiceEnvironmentIFCRef& env);
00284    void close();
00291    AssocDbHandle getHandle();
00292 
00293    typedef bool AssocDb::*safe_bool;
00297    operator safe_bool () const
00298       {  return m_opened ? &AssocDb::m_opened : 0; }
00299    bool operator!() const
00300       {  return !m_opened; }
00304    int getHandleCount() const { return m_hdlCount; }
00308    String getFileName() const { return m_fileName; }
00309 private:
00310    AssocDbEntry findEntry(const String& objectKey,
00311       AssocDbHandle& hdl);
00312    AssocDbEntry nextEntry(AssocDbHandle& hdl);
00313    void deleteEntry(const AssocDbEntry& entry, AssocDbHandle& hdl);
00314    void deleteEntry(const CIMObjectPath& objectName,
00315       const CIMName& assocClassName, const CIMName& resultClass,
00316       const CIMName& role, const CIMName& resultRole,
00317       const CIMObjectPath& associatedObject,
00318       const CIMObjectPath& assocClassPath, AssocDbHandle& hdl);
00319    void addEntry(const AssocDbEntry& entry, AssocDbHandle& hdl);
00320    void addEntry(const CIMObjectPath& objectName,
00321       const CIMName& assocClassName, const CIMName& resultClass,
00322       const CIMName& role, const CIMName& resultRole,
00323       const CIMObjectPath& associatedObject,
00324       const CIMObjectPath& assocClassPath, AssocDbHandle& hdl);
00325    void decHandleCount();
00326    MutexLock getDbLock() { return MutexLock(m_guard); }
00327    AssocDbEntry readEntry(Int32 offset, AssocDbHandle& hdl);
00328    void addToFreeList(Int32 offset, AssocDbHandle& hdl);
00329    AssocDbRecHeader getNewBlock(Int32& offset, UInt32 blkSize,
00330       AssocDbHandle& hdl);
00331    bool createFile();
00332    void checkFile();
00333 
00334 #ifdef OW_WIN32
00335 #pragma warning (push)
00336 #pragma warning (disable: 4251)
00337 #endif
00338 
00339    AssocDbHeader m_hdrBlock;
00340    IndexRef m_pIndex;
00341    String m_fileName;
00342    int m_hdlCount;
00343    bool m_opened;
00344    Mutex m_guard;
00345    ServiceEnvironmentIFCRef m_env;
00346    friend class AssocDbHandle;
00347    friend struct AssocDbHandle::AssocDbHandleData;
00348 
00349 #ifdef OW_WIN32
00350 #pragma warning (pop)
00351 #endif
00352 
00353 };
00354 
00355 } // end namespace OW_NAMESPACE
00356 
00357 #endif

Generated on Thu Feb 9 08:47:52 2006 for openwbem by  doxygen 1.4.6