/* ---- This file is part of SECONDO. Copyright (C) 2004-2009, University in Hagen, Faculty of Mathematics and Computer Science, Database Systems for New Applications. SECONDO is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. SECONDO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with SECONDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---- */ #pragma once #include "AlgebraTypes.h" #include "AttrArray.h" #include "Attribute.h" #include #include "Algebras/Geoid/Geoid.h" #include "NestedList.h" #include "ReadWrite.h" #include "SecondoSMI.h" #include "Shared.h" #include "SpatialAttrArray.h" #include namespace CRelAlgebra { class GAttrArrayInfo; class GAttrArrayIterator; /* Smartpointer to a ~GAttrArrayInfo~ */ typedef Shared PGAttrArrayInfo; /* Holds a ~GAttrArray~'s ~count~ and ~flobFileId~ values. Moving those values into this class allows writing and reading them with one operation and seperate storage (See ~AttrArray.Save~ and ~AttrArrayTypeConstructor.Load~). */ class GAttrArrayHeader { public: uint64_t count; SmiFileId flobFileId; /* Creates a ~GAttrArrayHeader~ with uninitialized values. */ GAttrArrayHeader(); /* Creates a ~GAttrArrayHeader~ with the provided ~count~ and ~flobFileId~. */ GAttrArrayHeader(uint64_t count, SmiFileId flobFileId); /* Creates a ~GAttrArrayHeader~ with the ~count~ and ~flobFileId~ from a ~AttrArrayHeader~. */ GAttrArrayHeader(const AttrArrayHeader &header); }; /* A generic ~AttrArray~ implementation. The attribute type is specified by a ~GAttrArrayInfo~. The ~Attribute~s are stored in a byte-array of their root blocks. ~Flob~s are either copied to a designated file (persistent ~GAttrArray~s) or left untouched (transient ~GAttrArray~s). */ class GAttrArray : public AttrArray { public: /* Creates a empty ~GAttrArray~ for the attribute type specified by ~info~. If persistence is desired a valid ~SmiFileId~ for ~Flob~ storage must be provided. */ GAttrArray(const PGAttrArrayInfo &info, SmiFileId flobFileId = 0); /* Restores a ~GAttrArray~ with attributes of the type specified by ~info~ from ~source~. Preconditions: *~source~ must hold data created by ~GAttrArray.Save~ with ~includeHeader~ == true. *~source~ must hold data created by a ~GAttrArray~ of equal attribute type */ GAttrArray(const PGAttrArrayInfo &info, Reader &source); /* Restores a ~GAttrArray~ with attributes of the type specified by ~info~ from ~source~ and ~header~. Preconditions: *~source~ must hold data created by ~GAttrArray.Save~ with ~includeHeader~ == false. *~source~ must hold data created by a ~GAttrArray~ of equal attribute type. *~header.count~ must equal the saved ~GAttrArray~'s row count. *~header.flobFile~ must equal the saved ~GAttrArray~'s flobFile count. */ GAttrArray(const PGAttrArrayInfo &info, Reader &source, const GAttrArrayHeader &header); GAttrArray(const GAttrArray &array, const SharedArray &filter); virtual ~GAttrArray(); virtual AttrArray *Filter(const SharedArray filter) const; /* Returns a shared pointer to the ~GAttrArrayInfo~ specifying this arrays attribute type. */ const PGAttrArrayInfo &GetInfo() const; //~AttrArray.GetCount~ virtual uint64_t GetCount() const; /* Returns the ammount of memory occupied ~Attribute~ root blocks. This does not include allocated but unused memory. */ //~AttrArray.GetSize~ virtual uint64_t GetSize() const; //~AttrArray.Save~ virtual void Save(Writer &target, bool includeHeader = true) const; //~AttrArray.DeleteRecords~ virtual void DeleteRecords(); //~AttrArray.Append~ virtual void Append(const AttrArray &array, uint64_t row); //~AttrArray.Append~ virtual void Append(Attribute &value); //~AttrArray.Remove~ virtual void Remove(); //~AttrArray.Clear~ virtual void Clear(); /* Returns the ~Attribute~ stored in the specified ~row~. Precondition: ~row~ < ~GetCount()~ */ Attribute &GetAt(uint64_t row) const; Attribute &operator[](uint64_t row) const; //AttrArray.IsDefined virtual bool IsDefined(uint64_t row) const; //AttrArray.Compare virtual int Compare(uint64_t rowA, const AttrArray &arrayB, uint64_t rowB) const; //AttrArray.Compare virtual int Compare(uint64_t row, Attribute &value) const; //AttrArray.CompareAlmost virtual int CompareAlmost(uint64_t rowA, const AttrArray &arrayB, uint64_t rowB) const; //AttrArray.CompareAlmost virtual int CompareAlmost(uint64_t row, Attribute &value) const; //AttrArray.Equals virtual bool Equals(uint64_t rowA, const AttrArray &arrayB, uint64_t rowB) const; //AttrArray.Equals virtual bool Equals(uint64_t row, Attribute &value) const; //AttrArray.GetHash virtual uint64_t GetHash(uint64_t row) const; //AttrArray.GetAttribute virtual Attribute *GetAttribute(uint64_t row, bool clone) const; /* Returns a ~GAttrArrayIterator~ over the stored attributes. */ GAttrArrayIterator GetIterator() const; /* Returns ~GAttrArrayIterator~s used (only!) for range-loop support. */ GAttrArrayIterator begin() const; GAttrArrayIterator end() const; private: friend class GAttrArrayIterator; GAttrArrayHeader m_header; const PGAttrArrayInfo m_info; uint64_t m_capacity, m_size; SharedArray m_data; char *m_attributesEnd; /* Deleted copy-constructor to prevent copying pointers. */ GAttrArray(const GAttrArray &array) = delete; }; /* Specifies and holds some info related to a ~GAttrArray~'s attribute type which can be shared among ~GattrArray~s. */ class GAttrArrayInfo { public: uint64_t attributeSize, attributeFLOBCount; ObjectCast attributeCastFunction; /* Creates a uninitialized ~GAttrArrayInfo~ */ GAttrArrayInfo(); /* Creates a ~GAttrArrayInfo~ specifying the provided attribute type. */ GAttrArrayInfo(ListExpr attributeTypeExpr); }; /* A iterator over the attributes of a ~GAttrArray~. Changes of the ~GAttrArray~ invalidate the iterator which is not reflected by ~GAttrArrayIterator.IsValid~. Further usage is considered undefined behaviour. The functions are defined in this header file to enable inlining. */ class GAttrArrayIterator { public: /* Creates a invalid iterator. */ GAttrArrayIterator() : m_instance(nullptr), m_attributeSize(0), m_current(nullptr), m_end(nullptr) { } /* Creates a iterator pointing at the first entry in the passed ~array~. If the ~array~ is empty the iterator is invalid. */ GAttrArrayIterator(const GAttrArray &array) : m_instance(&array), m_attributeSize(array.m_info->attributeSize), m_current(array.m_data.GetPointer()), m_end(array.m_attributesEnd) { } /* Determines if the iterator's current position is valid. */ bool IsValid() const { return m_current < m_end; } /* Returns a ~Attribute~ at the iterator's current position. Precondition: ~IsValid()~ */ Attribute &Get() { return *(Attribute*)m_current; } Attribute &operator * () { return Get(); } /* Moves the iterator to the next position. Returns true if that position is still valid. Precondition: ~IsValid()~ */ bool MoveToNext() { if (IsValid()) { m_current += m_attributeSize; return IsValid(); } return false; } /* Moves the iterator to the next position. Precondition: ~IsValid()~ */ GAttrArrayIterator &operator ++ () { MoveToNext(); return *this; } /* Compares this iterator and the ~other~ iterator for equality. */ bool operator == (const GAttrArrayIterator &other) const { return !(*this != other); } /* Compares this iterator and the ~other~ iterator for inequality. */ bool operator != (const GAttrArrayIterator &other) const { if (m_current < m_end) { if (other.m_current < other.m_end) { return m_current != other.m_current || m_instance != other.m_instance; } return true; } return other.m_current < other.m_end; } private: const GAttrArray *m_instance; uint64_t m_attributeSize; char *m_current, *m_end; }; template class GSpatialAttrArrayIterator; /* A generic ~SpatialAttrArray~ implementation. The attribute type is specified by a ~GAttrArrayInfo~. This class wraps a ~GAttrArray~ with a attribute type of kind SPATIAL1D, SPATIAL2D, SPATIAL3D, SPATIAL4D or SPATIAL8D. The fact that ~GAttrArray~ stores ~Attribute~s root blocks, allows to simply cast those into ~StandardSpatialAttribute~s. */ template class GSpatialAttrArray : public SpatialAttrArray { public: /* The constructors only call ~GAttrArray~'s constructor. The attribute type specified by ~info~ must be of kind SPATIAL1D, SPATIAL2D, SPATIAL3D, SPATIAL4D or SPATIAL8D. */ GSpatialAttrArray(const PGAttrArrayInfo &info); GSpatialAttrArray(const PGAttrArrayInfo &info, SmiFileId flobFileId); GSpatialAttrArray(const PGAttrArrayInfo &info, Reader &source); GSpatialAttrArray(const PGAttrArrayInfo &info, Reader &source, const GAttrArrayHeader &header); GSpatialAttrArray(const GSpatialAttrArray &array, const SharedArray &filter); virtual AttrArray *Filter(const SharedArray filter) const; //~AttrArray.GetCount~ virtual uint64_t GetCount() const; //~AttrArray.GetSize~ virtual uint64_t GetSize() const; //~AttrArray.Save~ virtual void Save(Writer &target, bool includeHeader = true) const; //~AttrArray.DeleteRecords~ virtual void DeleteRecords(); //~AttrArray.Append~ virtual void Append(const AttrArray &array, uint64_t row); //~AttrArray.Append~ virtual void Append(Attribute &value); //~AttrArray.Remove~ virtual void Remove(); //~AttrArray.Clear~ virtual void Clear(); /* Returns the ~StandardSpatialAttribute~ stored in the specified ~row~. Precondition: ~row~ < ~GetCount()~ */ StandardSpatialAttribute &GetAt(uint64_t index) const; StandardSpatialAttribute &operator[](uint64_t index) const; //~AttrArray.IsDefined~ virtual bool IsDefined(uint64_t row) const; //~AttrArray.Compare~ virtual int Compare(uint64_t rowA, const AttrArray &arrayB, uint64_t rowB) const; //~AttrArray.Compare~ virtual int Compare(uint64_t row, Attribute &value) const; //~AttrArray.CompareAlmost~ virtual int CompareAlmost(uint64_t rowA, const AttrArray &arrayB, uint64_t rowB) const; //~AttrArray.CompareAlmost~ virtual int CompareAlmost(uint64_t row, Attribute &value) const; //~AttrArray.Equals~ virtual bool Equals(uint64_t rowA, const AttrArray &arrayB, uint64_t rowB) const; //~AttrArray.Equals~ virtual bool Equals(uint64_t row, Attribute &value) const; //~AttrArray.EqualsAlmost~ virtual bool EqualsAlmost(uint64_t rowA, const AttrArray &arrayB, uint64_t rowB) const; //~AttrArray.EqualsAlmost~ virtual bool EqualsAlmost(uint64_t row, Attribute &value) const; //~AttrArray.GetHash~ virtual uint64_t GetHash(uint64_t row) const; //~AttrArray.GetAttribute~ virtual Attribute *GetAttribute(uint64_t row, bool clone) const; //~SpatialAttrArray.GetBoundingBox~ virtual Rectangle GetBoundingBox(uint64_t row, const Geoid* geoid = nullptr) const; //~SpatialAttrArray.GetDistance~ virtual double GetDistance(uint64_t row, const Rectangle& rect, const Geoid* geoid = nullptr) const; //~SpatialAttrArray.Intersects~ virtual bool Intersects(uint64_t row, const Rectangle& rect, const Geoid* geoid = nullptr) const; //~SpatialAttrArray.IsEmpty~ virtual bool IsEmpty(uint64_t row) const; /* Returns a ~GSpatialAttrArrayIterator~ over the stored attributes. */ GSpatialAttrArrayIterator GetIterator() const; /* Returns ~GSpatialAttrArrayIterator~s used (only!) for range-loop support. */ GSpatialAttrArrayIterator begin() const; GSpatialAttrArrayIterator end() const; private: friend class GSpatialAttrArrayIterator; GAttrArray m_array; }; /* A iterator over the attributes of a ~GSpatialAttrArray~. Changes of the ~GSpatialAttrArray~ invalidate the iterator which is not reflected by ~IsValid~. Further usage is considered undefined behaviour. The functions are defined in this header file to enable inlining. */ template class GSpatialAttrArrayIterator : public GAttrArrayIterator { public: /* Creates a invalid iterator. */ GSpatialAttrArrayIterator() { } /* Creates a iterator pointing at the first entry in the passed ~array~. If the ~array~ is empty the iterator is invalid. */ GSpatialAttrArrayIterator(const GSpatialAttrArray &array) : GAttrArrayIterator(array.m_array) { } /* Returns a ~StandardSpatialAttribute~ at the iterator's current position. Precondition: ~IsValid()~ */ StandardSpatialAttribute &Get() { return (StandardSpatialAttribute&)GAttrArrayIterator::Get(); } StandardSpatialAttribute &operator * () { return Get(); } }; }