xref: /core/connectivity/source/inc/dbase/DIndex.hxx (revision bd6fb0cd)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #pragma once
21 
22 #include <sdbcx/VIndex.hxx>
23 #include <dbase/DTable.hxx>
24 #include <dbase/dindexnode.hxx>
25 
26 inline constexpr OString dBASE_III_GROUP = "dBase III"_ostr;
27 
28 namespace connectivity::dbase
29     {
30         class OIndexIterator;
31         class ONDXKey;
32 
33         typedef sdbcx::OIndex ODbaseIndex_BASE;
34 
35         class ODbaseIndex : public ODbaseIndex_BASE
36         {
37             friend SvStream& WriteODbaseIndex(SvStream &rStream, const ODbaseIndex&);
38             friend SvStream& operator >> (SvStream &rStream, ODbaseIndex&);
39 
40             friend class ONDXNode;
41             friend class ONDXPage;
42             friend class ONDXPagePtr;
43             friend class OIndexIterator;
44 
45         public:
46 
47             // Header struct - stays in memory
48 
49             struct NDXHeader
50             {
51                 sal_uInt32  db_rootpage;                    /* Rootpage position                */
52                 sal_uInt32  db_pagecount;                   /* Page count                       */
53                 sal_uInt8   db_free[4];                     /* Reserved                         */
54                 sal_uInt16  db_keylen;                      /* Key length                       */
55                 sal_uInt16  db_maxkeys;                     /* Maximum number of keys per page  */
56                 sal_uInt16  db_keytype;                     /* Type of key:
57                                                                0 = Text
58                                                                1 = Numerical                    */
59                 sal_uInt16  db_keyrec;                      /* Length of an index record
60                                                                RecordNumber + keylen            */
61                 sal_uInt8   db_free1[3];                    /* Reserved                         */
62                 sal_uInt8   db_unique;                      /* Unique                           */
63                 char        db_name[488];                   /* index_name (field name)          */
64             };
65 
66         private:
67             std::unique_ptr<SvStream> m_pFileStream;        // Stream to read/write the index
68             NDXHeader       m_aHeader = {};
69             std::vector<ONDXPage*>
70                             m_aCollector;                   // Pool of obsolete pages
71             ONDXPagePtr     m_aRoot,                        // Root of the B+ tree
72                             m_aCurLeaf;                     // Current leaf
73             sal_uInt16      m_nCurNode;                     // Position of the current node
74 
75             sal_uInt32      m_nPageCount,
76                             m_nRootPage;
77 
78             ODbaseTable*    m_pTable;
79             bool        m_bUseCollector : 1;            // Use the Garbage Collector
80 
81             OUString getCompletePath() const;
82             void closeImpl();
83             // Closes and kills the index file and throws an error
84             void impl_killFileAndthrowError_throw(TranslateId pErrorId, const OUString& _sFile);
85         protected:
86             virtual ~ODbaseIndex() override;
87         public:
88             ODbaseIndex(ODbaseTable* _pTable);
89             ODbaseIndex(ODbaseTable* _pTable,const NDXHeader& _aHeader,const OUString& Name);
90 
91             void openIndexFile();
92             virtual void refreshColumns() override;
93 
getTable() const94             const ODbaseTable* getTable() const { return m_pTable; }
getHeader() const95             const NDXHeader& getHeader() const { return m_aHeader; }
96             std::unique_ptr<OIndexIterator> createIterator();
97 
SetRootPos(sal_uInt32 nPos)98             void SetRootPos(sal_uInt32 nPos)        {m_nRootPage = nPos;}
SetPageCount(sal_uInt32 nCount)99             void SetPageCount(sal_uInt32 nCount)    {m_nPageCount = nCount;}
100 
GetPageCount() const101             sal_uInt32 GetPageCount() const         {return m_nPageCount;}
102 
GetMaxNodes() const103             sal_uInt16 GetMaxNodes() const          {return m_aHeader.db_maxkeys;}
104 
105             bool Insert(sal_uInt32 nRec, const ORowSetValue& rValue);
106             bool Update(sal_uInt32 nRec, const ORowSetValue&, const ORowSetValue&);
107             bool Delete(sal_uInt32 nRec, const ORowSetValue& rValue);
108             bool Find(sal_uInt32 nRec, const ORowSetValue& rValue);
109 
110             void createINFEntry();
111             void CreateImpl();
112             void DropImpl();
113 
114             DECLARE_SERVICE_INFO();
115         protected:
116 
117             ONDXPage* CreatePage(sal_uInt32 nPagePos, ONDXPage* pParent = nullptr, bool bLoad = false);
118             void Collect(ONDXPage*);
119             ONDXPagePtr const & getRoot();
120 
isUnique() const121             bool isUnique() const { return m_IsUnique; }
UseCollector() const122             bool UseCollector() const {return m_bUseCollector;}
123             // Tree operations
124             void Release(bool bSave = true);
125             bool ConvertToKey(ONDXKey* rKey, sal_uInt32 nRec, const ORowSetValue& rValue);
126         };
127 
128         SvStream& WriteODbaseIndex(SvStream &rStream, const ODbaseIndex&);
129         SvStream& operator >> (SvStream &rStream, ODbaseIndex&);
130 
131         void ReadHeader(SvStream & rStream, ODbaseIndex::NDXHeader & rHeader);
132 }
133 
134 
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
136