Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

596 lines
13 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. database.h
  5. Abstract:
  6. SIS Groveler Jet-Blue database include file
  7. Authors:
  8. Cedric Krumbein, 1998
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #define USERNAME ""
  14. #define PASSWORD ""
  15. #define TABLE_PAGES 50
  16. #define TABLE_DENSITY 50
  17. #define MIN_VER_PAGES 128
  18. #define MAX_DATABASE_CACHE_SIZE (6*1024*1024)
  19. #define COUNTRY_CODE 1
  20. #define LANG_ID 0x0409
  21. #define CODE_PAGE 1252
  22. #define COLLATE 0
  23. #define MAX_KEYS 3
  24. #define MAX_COLUMNS 8
  25. #define MAX_LIST_NAME_LEN 255
  26. #define GET_ALL_MASK (~0U)
  27. typedef struct {
  28. CHAR *name;
  29. DWORD size;
  30. DWORD offset;
  31. JET_COLTYP coltyp;
  32. JET_GRBIT grbit;
  33. } ColumnSpec;
  34. /***************************** Table parameters ******************************/
  35. #define TABLE_NAME "SGTable"
  36. typedef DWORDLONG Signature;
  37. typedef GUID CSID;
  38. typedef struct _SGNativeTableEntry {
  39. DWORDLONG fileID;
  40. DWORDLONG fileSize;
  41. Signature signature;
  42. DWORD attributes;
  43. CSID csIndex;
  44. DWORDLONG createTime;
  45. DWORDLONG writeTime;
  46. } SGNativeTableEntry;
  47. #define TABLE_COL_NUM_FILE_ID 0
  48. #define TABLE_COL_NUM_SIZE 1
  49. #define TABLE_COL_NUM_SIG 2
  50. #define TABLE_COL_NUM_ATTR 3
  51. #define TABLE_COL_NUM_CSIDLO 4
  52. #define TABLE_COL_NUM_CSIDHI 5
  53. #define TABLE_COL_NUM_CR_TIME 6
  54. #define TABLE_COL_NUM_WR_TIME 7
  55. #define TABLE_NCOLS 8
  56. #define TABLE_COL_NAME_FILE_ID "FileID"
  57. #define TABLE_COL_NAME_SIZE "FileSize"
  58. #define TABLE_COL_NAME_SIG "Signature"
  59. #define TABLE_COL_NAME_ATTR "Attributes"
  60. #define TABLE_COL_NAME_CSIDLO "CSIndexLo"
  61. #define TABLE_COL_NAME_CSIDHI "CSIndexHi"
  62. #define TABLE_COL_NAME_CR_TIME "CreateTime"
  63. #define TABLE_COL_NAME_WR_TIME "WriteTime"
  64. #define TABLE_KEY_NAME_FILE_ID "FileIDKey"
  65. #define TABLE_KEY_NAME_ATTR "AttributeKey"
  66. #define TABLE_KEY_NAME_CSID "CSIndexKey"
  67. #define TABLE_KEY_NCOLS_FILE_ID 1
  68. #define TABLE_KEY_NCOLS_ATTR 3
  69. #define TABLE_KEY_NCOLS_CSID 2
  70. #define TABLE_EXCLUDE_FILE_ID_MASK \
  71. (~(1U << TABLE_COL_NUM_FILE_ID))
  72. #define TABLE_EXCLUDE_ATTR_MASK \
  73. (~(1U << TABLE_COL_NUM_SIZE \
  74. | 1U << TABLE_COL_NUM_SIG \
  75. | 1U << TABLE_COL_NUM_ATTR))
  76. #define TABLE_EXCLUDE_CS_INDEX_MASK \
  77. (~(1U << TABLE_COL_NUM_CSIDLO \
  78. | 1U << TABLE_COL_NUM_CSIDHI))
  79. static ColumnSpec
  80. tableColDefFileID = {
  81. TABLE_COL_NAME_FILE_ID,
  82. sizeof(DWORDLONG),
  83. offsetof(SGNativeTableEntry, fileID),
  84. JET_coltypCurrency,
  85. 0
  86. },
  87. tableColDefSize = {
  88. TABLE_COL_NAME_SIZE,
  89. sizeof(DWORDLONG),
  90. offsetof(SGNativeTableEntry, fileSize),
  91. JET_coltypCurrency,
  92. 0
  93. },
  94. tableColDefSig = {
  95. TABLE_COL_NAME_SIG,
  96. sizeof(Signature),
  97. offsetof(SGNativeTableEntry, signature),
  98. JET_coltypCurrency,
  99. 0
  100. },
  101. tableColDefAttr = {
  102. TABLE_COL_NAME_ATTR,
  103. sizeof(DWORD),
  104. offsetof(SGNativeTableEntry, attributes),
  105. JET_coltypLong,
  106. 0
  107. },
  108. tableColDefCSIDlo = {
  109. TABLE_COL_NAME_CSIDLO,
  110. sizeof(DWORDLONG),
  111. offsetof(SGNativeTableEntry, csIndex),
  112. JET_coltypCurrency,
  113. 0
  114. },
  115. tableColDefCSIDhi = {
  116. TABLE_COL_NAME_CSIDHI,
  117. sizeof(DWORDLONG),
  118. offsetof(SGNativeTableEntry, csIndex) + sizeof(DWORDLONG),
  119. JET_coltypCurrency,
  120. 0
  121. },
  122. tableColDefCrTime = {
  123. TABLE_COL_NAME_CR_TIME,
  124. sizeof(DWORDLONG),
  125. offsetof(SGNativeTableEntry, createTime),
  126. JET_coltypCurrency,
  127. 0
  128. },
  129. tableColDefWrTime = {
  130. TABLE_COL_NAME_WR_TIME,
  131. sizeof(DWORDLONG),
  132. offsetof(SGNativeTableEntry, writeTime),
  133. JET_coltypCurrency,
  134. 0
  135. },
  136. *tableColumnSpecs[TABLE_NCOLS] = {
  137. &tableColDefFileID,
  138. &tableColDefSize,
  139. &tableColDefSig,
  140. &tableColDefAttr,
  141. &tableColDefCSIDlo,
  142. &tableColDefCSIDhi,
  143. &tableColDefCrTime,
  144. &tableColDefWrTime
  145. },
  146. *tableKeyFileID[TABLE_KEY_NCOLS_FILE_ID] = {
  147. &tableColDefFileID
  148. },
  149. *tableKeyAttr[TABLE_KEY_NCOLS_ATTR] = {
  150. &tableColDefSize,
  151. &tableColDefSig,
  152. &tableColDefAttr,
  153. },
  154. *tableKeyCSID[TABLE_KEY_NCOLS_CSID] = {
  155. &tableColDefCSIDlo,
  156. &tableColDefCSIDhi,
  157. };
  158. /***************************** Queue parameters ******************************/
  159. #define QUEUE_NAME "SGQueue"
  160. typedef struct _SGNativeQueueEntry {
  161. DWORDLONG fileID;
  162. DWORDLONG parentID;
  163. DWORD reason;
  164. DWORD order;
  165. DWORDLONG readyTime;
  166. DWORDLONG retryTime;
  167. TCHAR *fileName;
  168. } SGNativeQueueEntry;
  169. #define QUEUE_COL_NUM_FILE_ID 0
  170. #define QUEUE_COL_NUM_PARENT_ID 1
  171. #define QUEUE_COL_NUM_REASON 2
  172. #define QUEUE_COL_NUM_ORDER 3
  173. #define QUEUE_COL_NUM_READY_TIME 4
  174. #define QUEUE_COL_NUM_RETRY_TIME 5
  175. #define QUEUE_COL_NUM_NAME 6
  176. #define QUEUE_NCOLS 7
  177. #define QUEUE_COL_NAME_FILE_ID "FileID"
  178. #define QUEUE_COL_NAME_PARENT_ID "ParentID"
  179. #define QUEUE_COL_NAME_REASON "Reason"
  180. #define QUEUE_COL_NAME_ORDER "Order"
  181. #define QUEUE_COL_NAME_READY_TIME "ReadyTime"
  182. #define QUEUE_COL_NAME_RETRY_TIME "RetryTime"
  183. #define QUEUE_COL_NAME_NAME "FileName"
  184. #define QUEUE_KEY_NAME_READY_TIME "ReadyTimeKey"
  185. #define QUEUE_KEY_NAME_FILE_ID "FileIDKey"
  186. #define QUEUE_KEY_NAME_ORDER "Order"
  187. #define QUEUE_KEY_NCOLS_READY_TIME 1
  188. #define QUEUE_KEY_NCOLS_FILE_ID 1
  189. #define QUEUE_KEY_NCOLS_ORDER 1
  190. #define QUEUE_EXCLUDE_FILE_ID_MASK \
  191. (~(1U << QUEUE_COL_NUM_FILE_ID))
  192. static ColumnSpec
  193. queueColDefFileID = {
  194. QUEUE_COL_NAME_FILE_ID,
  195. sizeof(DWORDLONG),
  196. offsetof(SGNativeQueueEntry, fileID),
  197. JET_coltypCurrency,
  198. 0
  199. },
  200. queueColDefParentID = {
  201. QUEUE_COL_NAME_PARENT_ID,
  202. sizeof(DWORDLONG),
  203. offsetof(SGNativeQueueEntry, parentID),
  204. JET_coltypCurrency,
  205. 0
  206. },
  207. queueColDefReason = {
  208. QUEUE_COL_NAME_REASON,
  209. sizeof(DWORD),
  210. offsetof(SGNativeQueueEntry, reason),
  211. JET_coltypLong,
  212. 0
  213. },
  214. queueColDefOrder = {
  215. QUEUE_COL_NAME_ORDER,
  216. sizeof(DWORD),
  217. offsetof(SGNativeQueueEntry, order),
  218. JET_coltypLong,
  219. JET_bitColumnAutoincrement
  220. },
  221. queueColDefReadyTime = {
  222. QUEUE_COL_NAME_READY_TIME,
  223. sizeof(DWORDLONG),
  224. offsetof(SGNativeQueueEntry, readyTime),
  225. JET_coltypCurrency,
  226. 0
  227. },
  228. queueColDefRetryTime = {
  229. QUEUE_COL_NAME_RETRY_TIME,
  230. sizeof(DWORDLONG),
  231. offsetof(SGNativeQueueEntry, retryTime),
  232. JET_coltypCurrency,
  233. 0
  234. },
  235. queueColDefName = {
  236. QUEUE_COL_NAME_NAME,
  237. MAX_PATH * sizeof(TCHAR),
  238. offsetof(SGNativeQueueEntry, fileName),
  239. JET_coltypLongBinary,
  240. 0
  241. },
  242. *queueColumnSpecs[QUEUE_NCOLS] = {
  243. &queueColDefFileID,
  244. &queueColDefParentID,
  245. &queueColDefReason,
  246. &queueColDefOrder,
  247. &queueColDefReadyTime,
  248. &queueColDefRetryTime,
  249. &queueColDefName
  250. },
  251. *queueKeyReadyTime[QUEUE_KEY_NCOLS_READY_TIME] = {
  252. &queueColDefReadyTime
  253. },
  254. *queueKeyFileID[QUEUE_KEY_NCOLS_FILE_ID] = {
  255. &queueColDefFileID
  256. },
  257. *queueKeyOrder[QUEUE_KEY_NCOLS_ORDER] = {
  258. &queueColDefOrder
  259. };
  260. /***************************** Stack parameters ******************************/
  261. #define STACK_NAME "SGStack"
  262. typedef struct _SGNativeStackEntry {
  263. DWORDLONG fileID;
  264. DWORD order;
  265. } SGNativeStackEntry;
  266. #define STACK_COL_NUM_FILE_ID 0
  267. #define STACK_COL_NUM_ORDER 1
  268. #define STACK_NCOLS 2
  269. #define STACK_COL_NAME_FILE_ID "FileID"
  270. #define STACK_COL_NAME_ORDER "Order"
  271. #define STACK_KEY_NAME_FILE_ID "FileIDKey"
  272. #define STACK_KEY_NAME_ORDER "Order"
  273. #define STACK_KEY_NCOLS_FILE_ID 1
  274. #define STACK_KEY_NCOLS_ORDER 1
  275. #define STACK_EXCLUDE_FILE_ID_MASK \
  276. (~(1U << STACK_COL_NUM_FILE_ID))
  277. #define STACK_GET_ORDER_ONLY_MASK \
  278. (1U << STACK_COL_NUM_ORDER)
  279. static ColumnSpec
  280. stackColDefFileID = {
  281. STACK_COL_NAME_FILE_ID,
  282. sizeof(DWORDLONG),
  283. offsetof(SGNativeStackEntry, fileID),
  284. JET_coltypCurrency,
  285. 0
  286. },
  287. stackColDefOrder = {
  288. STACK_COL_NAME_ORDER,
  289. sizeof(DWORD),
  290. offsetof(SGNativeStackEntry, order),
  291. JET_coltypLong,
  292. 0
  293. },
  294. *stackColumnSpecs[STACK_NCOLS] = {
  295. &stackColDefFileID,
  296. &stackColDefOrder
  297. },
  298. *stackKeyFileID[STACK_KEY_NCOLS_FILE_ID] = {
  299. &stackColDefFileID
  300. },
  301. *stackKeyOrder[STACK_KEY_NCOLS_ORDER] = {
  302. &stackColDefOrder
  303. };
  304. /****************************** List parameters ******************************/
  305. #define LIST_NAME "SGList"
  306. typedef struct _SGNativeListEntry {
  307. const TCHAR *name;
  308. TCHAR *value;
  309. } SGNativeListEntry;
  310. #define LIST_COL_NUM_NAME 0
  311. #define LIST_COL_NUM_VALUE 1
  312. #define LIST_NCOLS 2
  313. #define LIST_COL_NAME_NAME "Name"
  314. #define LIST_COL_NAME_VALUE "Value"
  315. #define LIST_KEY_NAME_NAME "NameKey"
  316. #define LIST_KEY_NCOLS_NAME 1
  317. #define LIST_EXCLUDE_NAME_MASK \
  318. (~(1U << LIST_COL_NUM_NAME))
  319. static ColumnSpec
  320. listColDefName = {
  321. LIST_COL_NAME_NAME,
  322. MAX_LIST_NAME_LEN,
  323. offsetof(SGNativeListEntry, name),
  324. JET_coltypBinary,
  325. 0
  326. },
  327. listColDefValue = {
  328. LIST_COL_NAME_VALUE,
  329. MAX_PATH * sizeof(TCHAR),
  330. offsetof(SGNativeListEntry, value),
  331. JET_coltypLongBinary,
  332. 0
  333. },
  334. *listColumnSpecs[LIST_NCOLS] = {
  335. &listColDefName,
  336. &listColDefValue,
  337. },
  338. *listKeyName[LIST_KEY_NCOLS_NAME] = {
  339. &listColDefName
  340. };
  341. /************************ SGDatabase class declaration ***********************/
  342. class SGDatabase {
  343. private:
  344. static DWORD numInstances;
  345. static JET_INSTANCE instance;
  346. static BOOL jetInitialized;
  347. static TCHAR *logDir;
  348. CHAR *fileName; // fully qualified database file name
  349. JET_SESID sesID;
  350. JET_DBID dbID;
  351. JET_TABLEID tableID,
  352. queueID,
  353. stackID,
  354. listID;
  355. JET_COLUMNID tableColumnIDs[TABLE_NCOLS],
  356. queueColumnIDs[QUEUE_NCOLS],
  357. stackColumnIDs[STACK_NCOLS],
  358. listColumnIDs[ LIST_NCOLS];
  359. LONG numTableEntries,
  360. numQueueEntries,
  361. numStackEntries,
  362. numListEntries,
  363. numUncommittedTableEntries,
  364. numUncommittedQueueEntries,
  365. numUncommittedStackEntries,
  366. numUncommittedListEntries;
  367. BOOL inTransaction;
  368. // Static methods
  369. static BOOL InitializeEngine();
  370. static BOOL TerminateEngine();
  371. static void CleanupEngineFiles();
  372. static void DeleteAllDatabaseFiles();
  373. // Table / queue / stack / list methods
  374. BOOL CreateTable(
  375. const CHAR *tblName,
  376. DWORD numColumns,
  377. ColumnSpec **columnSpecs,
  378. JET_COLUMNID *columnIDs,
  379. JET_TABLEID *tblID);
  380. BOOL CreateIndex(
  381. JET_TABLEID tblID,
  382. const CHAR *keyName,
  383. DWORD numKeys,
  384. ColumnSpec **keyColumnSpecs);
  385. BOOL OpenTable(
  386. const CHAR *tblName,
  387. DWORD numColumns,
  388. ColumnSpec **columnSpecs,
  389. JET_COLUMNID *columnIDs,
  390. JET_TABLEID *tblID);
  391. BOOL CloseTable(JET_TABLEID tblID);
  392. LONG PositionCursor(
  393. JET_TABLEID tblID,
  394. const CHAR *keyName,
  395. const VOID *entry,
  396. DWORD numKeys,
  397. ColumnSpec **keyColumnSpecs) const;
  398. LONG PositionCursorFirst(
  399. JET_TABLEID tblID,
  400. const CHAR *keyName) const;
  401. LONG PositionCursorNext(JET_TABLEID tblID) const;
  402. LONG PositionCursorLast(
  403. JET_TABLEID tblID,
  404. const CHAR *keyName) const;
  405. BOOL PutData(
  406. JET_TABLEID tblID,
  407. const VOID *entry,
  408. DWORD numColumns,
  409. ColumnSpec **columnSpecs,
  410. const JET_COLUMNID *columnIDs);
  411. BOOL SGDatabase::RetrieveData(
  412. JET_TABLEID tblID,
  413. VOID *entry,
  414. DWORD numColumns,
  415. ColumnSpec **columnSpecs,
  416. const JET_COLUMNID *columnIDs,
  417. DWORD includeMask) const;
  418. LONG Delete(JET_TABLEID tblID);
  419. LONG Count(
  420. JET_TABLEID tblID,
  421. const CHAR *keyName) const;
  422. public:
  423. // General methods
  424. SGDatabase();
  425. ~SGDatabase();
  426. BOOL Create(const TCHAR *dbName);
  427. BOOL Open(
  428. const TCHAR *drive_LetterName,
  429. const TCHAR *dbName,
  430. BOOL is_log_drive);
  431. BOOL Close();
  432. BOOL BeginTransaction();
  433. BOOL CommitTransaction();
  434. BOOL AbortTransaction();
  435. static BOOL set_log_drive(const _TCHAR *drive_name);
  436. // Table methods
  437. LONG TablePut(const SGNativeTableEntry *entry);
  438. LONG TableGetFirstByFileID(SGNativeTableEntry *entry) const;
  439. LONG TableGetFirstByAttr(SGNativeTableEntry *entry) const;
  440. LONG TableGetFirstByCSIndex(SGNativeTableEntry *entry) const;
  441. LONG TableGetNext(SGNativeTableEntry *entry) const;
  442. LONG TableDeleteByFileID(DWORDLONG fileID);
  443. LONG TableDeleteByCSIndex(const CSID *csIndex);
  444. LONG TableCount() const;
  445. // Queue methods
  446. LONG QueuePut(SGNativeQueueEntry *entry);
  447. LONG QueueGetFirst(SGNativeQueueEntry *entry) const;
  448. LONG QueueGetFirstByFileID(SGNativeQueueEntry *entry) const;
  449. LONG QueueGetNext(SGNativeQueueEntry *entry) const;
  450. LONG QueueDelete(DWORD order);
  451. LONG QueueDeleteByFileID(DWORDLONG fileID);
  452. LONG QueueCount() const;
  453. // Stack methods
  454. LONG StackPut(DWORDLONG fileID, BOOL done);
  455. LONG StackGetTop(SGNativeStackEntry *entry) const;
  456. LONG StackGetFirstByFileID(SGNativeStackEntry *entry) const;
  457. LONG StackGetNext(SGNativeStackEntry *entry) const;
  458. LONG StackDelete(DWORD order);
  459. LONG StackDeleteByFileID(DWORDLONG fileID);
  460. LONG StackCount() const;
  461. // List methods
  462. LONG ListWrite(const SGNativeListEntry *entry);
  463. LONG ListRead(SGNativeListEntry *entry) const;
  464. LONG ListDelete(const TCHAR *name);
  465. LONG ListCount() const;
  466. };