ECGenericObjectTable.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef ECGENERIC_OBJECTTABLE_H
  18. #define ECGENERIC_OBJECTTABLE_H
  19. #define MAX_SORTKEY_LEN 4096
  20. #include <kopano/zcdefs.h>
  21. #include <mutex>
  22. #include "soapH.h"
  23. #include <list>
  24. #include <map>
  25. #include "ECSubRestriction.h"
  26. #include <kopano/ECKeyTable.h>
  27. #include "ECDatabase.h"
  28. #include <kopano/ustringutil.h>
  29. #include <kopano/ECUnknown.h>
  30. struct soap;
  31. namespace KC {
  32. /*
  33. * This object is an actual table, with a cursor in-memory. We also keep the complete
  34. * keyset of the table in memory, so seeks and queries can be really fast. Also, we
  35. * sort the table once on loading, and simply use that keyset when the rows are actually
  36. * required. The actual data is always loaded directly from the database.
  37. */
  38. typedef std::list<unsigned int> ECListInt;
  39. typedef std::list<unsigned int>::const_iterator ECListIntIterator;
  40. #define RESTRICT_MAX_DEPTH 16
  41. #define OBJECTTABLE_NOTIFY 1
  42. class ECSession;
  43. class ECCacheManager;
  44. typedef std::map<ECTableRow, sObjectTableKey> ECSortedCategoryMap;
  45. class ECCategory _kc_final {
  46. public:
  47. ECCategory(unsigned int ulCategory, struct propVal *lpProps, unsigned int cProps, unsigned int nProps, ECCategory *lpParent, unsigned int ulDepth, bool fExpanded, const ECLocale &locale);
  48. virtual ~ECCategory();
  49. void IncLeaf();
  50. void DecLeaf();
  51. void IncUnread();
  52. void DecUnread();
  53. unsigned int GetCount(void) const { return m_ulLeafs; }
  54. ECCategory* GetParent();
  55. ECRESULT GetProp(struct soap* soap, unsigned int ulPropTag, struct propVal *lpPropVal);
  56. ECRESULT SetProp(unsigned int i, struct propVal *lpPropVal);
  57. ECRESULT UpdateMinMax(const sObjectTableKey &sKey, unsigned int i, struct propVal *lpPropVal, bool fMax, bool *lpfModified);
  58. ECRESULT UpdateMinMaxRemove(const sObjectTableKey &sKey, unsigned int i, bool fMax, bool *lpfModified);
  59. size_t GetObjectSize(void) const;
  60. struct propVal *m_lpProps;
  61. unsigned int m_cProps;
  62. ECCategory *m_lpParent;
  63. unsigned int m_ulDepth;
  64. unsigned int m_ulCategory;
  65. bool m_fExpanded;
  66. unsigned int m_ulUnread = 0, m_ulLeafs = 0;
  67. const ECLocale& m_locale;
  68. std::map<sObjectTableKey, struct propVal *> m_mapMinMax;
  69. ECSortedCategoryMap::iterator iSortedCategory;
  70. sObjectTableKey m_sCurMinMax;
  71. };
  72. struct LEAFINFO {
  73. bool fUnread;
  74. ECCategory *lpCategory;
  75. };
  76. typedef std::map<sObjectTableKey, ECCategory *, ObjectTableKeyCompare> ECCategoryMap;
  77. typedef std::map<sObjectTableKey, LEAFINFO, ObjectTableKeyCompare> ECLeafMap;
  78. class ECGenericObjectTable;
  79. typedef ECRESULT (* QueryRowDataCallBack)(ECGenericObjectTable *lpThis, struct soap *soap, ECSession *lpSession, ECObjectTableList* lpRowList, struct propTagArray *lpsPropTagArray, void* lpObjectData, struct rowSet **lppRowSet, bool bTableData, bool bTableLimit);
  80. /*
  81. * ECGenericObjectTable
  82. *
  83. * Some things about adding rows:
  84. *
  85. * There are a few functions that are used to add rows, each at a different level. Here is a quick description of each
  86. * function, ordered in call-stack-order (top calls bottom)
  87. *
  88. * UpdateRow(type, objectid, flags)
  89. * - Checks security for the object
  90. * - Updates ALL rows for a specific object ID (usually one row since MVI is not being used)
  91. * AddRowKey(list_of_row_ids, number_of_rows_added *, flags)
  92. * - Retrieves the sort data for all rows to be added
  93. * - Applies restriction
  94. * AddRow(row_id, <sort data>, flags, hidden)
  95. * - Adds an actual row to the table
  96. * - Updates category headers
  97. *
  98. * During a row deletion, the second layer is skipped since you don't need to retrieve the sort info or do restrictions
  99. * on deleted rows, and UpdateRow() directly calls DeleteRow(), which is analogous to AddRow().
  100. *
  101. * Doing a re-sort deletes all rows and re-adds them from the 'AddRowKey' level.
  102. *
  103. * @todo Support more then one multi-value instance property. (Never saw this from Outlook)
  104. */
  105. class ECGenericObjectTable : public ECUnknown {
  106. public:
  107. ECGenericObjectTable(ECSession *lpSession, unsigned int ulObjType, unsigned int ulFlags, const ECLocale &locale);
  108. virtual ~ECGenericObjectTable();
  109. /**
  110. * An enumeration for the table reload type.
  111. * The table reload type determines how it should changed
  112. */
  113. enum enumReloadType {
  114. RELOAD_TYPE_SETCOLUMNS, /**< Reload table because changed columns */
  115. RELOAD_TYPE_SORTORDER /**< Reload table because changed sort order */
  116. };
  117. // Client operations
  118. ECRESULT SeekRow(unsigned int ulBookmark, int lSeekTo, int *lplRowsSought);
  119. ECRESULT FindRow(struct restrictTable *lpsRestrict, unsigned int ulBookmark, unsigned int ulFlags);
  120. ECRESULT GetRowCount(unsigned int *lpulRowCount, unsigned int *lpulCurrentRow);
  121. ECRESULT SetColumns(const struct propTagArray *lpsPropTags, bool bDefaultSet);
  122. ECRESULT GetColumns(struct soap *soap, ULONG ulFlags, struct propTagArray **lpsPropTags);
  123. ECRESULT SetSortOrder(struct sortOrderArray *, unsigned int categ, unsigned int expanded) __attribute__((nonnull));
  124. ECRESULT Restrict(struct restrictTable *lpsRestrict);
  125. ECRESULT QueryRows(struct soap *soap, unsigned int ulRowCount, unsigned int ulFlags, struct rowSet **lppRowSet);
  126. ECRESULT CreateBookmark(unsigned int* lpulbkPosition);
  127. ECRESULT FreeBookmark(unsigned int ulbkPosition);
  128. ECRESULT ExpandRow(struct soap *soap, xsd__base64Binary sInstanceKey, unsigned int ulRowCount, unsigned int ulFlags, struct rowSet **lppRowSet, unsigned int *lpulRowsLeft);
  129. ECRESULT CollapseRow(xsd__base64Binary sInstanceKey, unsigned int ulFlags, unsigned int *lpulRows);
  130. ECRESULT GetCollapseState(struct soap *soap, struct xsd__base64Binary sBookmark, struct xsd__base64Binary *lpsCollapseState);
  131. ECRESULT SetCollapseState(struct xsd__base64Binary sCollapseState, unsigned int *lpulBookmark);
  132. virtual ECRESULT GetColumnsAll(ECListInt* lplstProps);
  133. virtual ECRESULT ReloadTableMVData(ECObjectTableList* lplistRows, ECListInt* lplistMVPropTag);
  134. // Sort functions
  135. virtual ECRESULT IsSortKeyExist(const sObjectTableKey* lpsRowItem, unsigned int ulPropTag);
  136. virtual ECRESULT GetSortKey(sObjectTableKey* lpsRowItem, unsigned int ulPropTag, unsigned int *lpSortLen, unsigned char **lppSortData);
  137. virtual ECRESULT SetSortKey(sObjectTableKey* lpsRowItem, unsigned int ulPropTag, unsigned int ulSortLen, unsigned char *lpSortData);
  138. // Server operations
  139. virtual ECRESULT Clear();
  140. virtual ECRESULT Populate();
  141. virtual ECRESULT UpdateRow(unsigned int ulType, unsigned int ulObjId, unsigned int ulFlags);
  142. virtual ECRESULT UpdateRows(unsigned int ulType, std::list<unsigned int> *lstObjId, unsigned int ulFlags, bool bInitialLoad);
  143. virtual ECRESULT LoadRows(std::list<unsigned int> *lstObjId, unsigned int ulFlags);
  144. static ECRESULT GetRestrictPropTagsRecursive(struct restrictTable *lpsRestrict, std::list<ULONG> *lpPropTags, ULONG ulLevel);
  145. static ECRESULT GetRestrictPropTags(struct restrictTable *lpsRestrict, std::list<ULONG> *lpPrefixTags, struct propTagArray **lppPropTags);
  146. static ECRESULT MatchRowRestrict(ECCacheManager* lpCacheManager, struct propValArray *lpPropVals, struct restrictTable *lpsRestruct, SUBRESTRICTIONRESULTS *lpSubResults, const ECLocale &locale, bool *fMatch, unsigned int *lpulSubRestriction = NULL);
  147. virtual ECRESULT GetComputedDepth(struct soap *soap, ECSession *lpSession, unsigned int ulObjId, struct propVal *lpProp);
  148. bool IsMVSet();
  149. void SetTableId(unsigned int ulTableId);
  150. virtual ECRESULT GetPropCategory(struct soap *soap, unsigned int ulPropTag, sObjectTableKey sKey, struct propVal *lpPropVal);
  151. virtual unsigned int GetCategories();
  152. virtual size_t GetObjectSize(void);
  153. protected:
  154. // Add an actual row to the table, and send a notification if required. If you add an existing row, the row is modified and the notification is send as a modification
  155. ECRESULT AddRow(sObjectTableKey sRowItem, struct propVal *lpProps, unsigned int cProps, unsigned int ulFlags, bool fHidden, ECCategory *lpCategory);
  156. // Remove an actual row from the table, and send a notification if required. You may try to delete non-existing rows, in which case nothing happens
  157. ECRESULT DeleteRow(sObjectTableKey sRow, unsigned int ulFlags);
  158. // Send a notification by getting row data and adding the notification
  159. ECRESULT AddTableNotif(ECKeyTable::UpdateType, sObjectTableKey sRowItem, sObjectTableKey *lpsPrevRow);
  160. // Add data to key table
  161. ECRESULT UpdateKeyTableRow(ECCategory *lpCategory, sObjectTableKey *lpsRowKey, struct propVal *lpProps, unsigned int cValues, bool fHidden, sObjectTableKey *sPrevRow, ECKeyTable::UpdateType *lpulAction);
  162. // Update min/max field for category
  163. ECRESULT UpdateCategoryMinMax(sObjectTableKey& lpKey, ECCategory *lpCategory, size_t i, struct propVal *lpProps, size_t cProps, bool *lpfModified);
  164. virtual ECRESULT ReloadKeyTable();
  165. ECRESULT GetBinarySortKey(struct propVal *lpsPropVal, unsigned int *lpSortLen, unsigned char **lppSortData);
  166. ECRESULT GetSortFlags(unsigned int ulPropTag, unsigned char *lpFlags);
  167. virtual ECRESULT GetMVRowCount(unsigned int ulObjId, unsigned int *lpulCount);
  168. virtual ECRESULT ReloadTable(enumReloadType eType);
  169. virtual ECRESULT Load();
  170. virtual ECRESULT CheckPermissions(unsigned int ulObjId); // normally overridden by subclass
  171. const ECLocale &GetLocale() const { return m_locale; }
  172. // Constants
  173. ECSession* lpSession;
  174. ECKeyTable* lpKeyTable;
  175. unsigned int m_ulTableId = -1; /* id of the table from ECTableManager */
  176. void *m_lpObjectData = nullptr;
  177. std::recursive_mutex m_hLock; /* Lock for locked internals */
  178. // Locked internals
  179. struct sortOrderArray *lpsSortOrderArray = nullptr; /* Stored sort order */
  180. struct propTagArray *lpsPropTagArray = nullptr; /* Stored column set */
  181. struct restrictTable *lpsRestrict = nullptr; /* Stored restriction */
  182. ECObjectTableMap mapObjects; // Map of all objects in this table
  183. ECListInt m_listMVSortCols; // List of MV sort columns
  184. bool m_bMVCols = false; /* Are there MV props in the column list */
  185. bool m_bMVSort = false; /* Are there MV props in the sort order */
  186. unsigned int m_ulObjType;
  187. unsigned int m_ulFlags; //< flags from client
  188. QueryRowDataCallBack m_lpfnQueryRowData = nullptr;
  189. virtual ECRESULT AddRowKey(ECObjectTableList* lpRows, unsigned int *lpulLoaded, unsigned int ulFlags, bool bInitialLoad, bool bOverride, struct restrictTable *lpOverrideRestrict);
  190. virtual ECRESULT AddCategoryBeforeAddRow(sObjectTableKey sObjKey, struct propVal *lpProps, unsigned int cProps, unsigned int ulFlags, bool fUnread, bool *lpfHidden, ECCategory **lppCategory);
  191. virtual ECRESULT RemoveCategoryAfterRemoveRow(sObjectTableKey sObjKey, unsigned int ulFlags);
  192. ECCategoryMap m_mapCategories; // Map between instance key of category and category struct
  193. ECSortedCategoryMap m_mapSortedCategories; // Map between category sort keys and instance key. This is where we track which categories we have
  194. ECLeafMap m_mapLeafs; // Map between object instance key and LEAFINFO (contains unread flag and category pointer)
  195. unsigned int m_ulCategory = 1, m_ulCategories = 0;
  196. unsigned int m_ulExpanded = 0;
  197. bool m_bPopulated = false;
  198. ECLocale m_locale;
  199. };
  200. } /* namespace */
  201. #endif // ECGENERIC_OBJECTTABLE_H