home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / test / testlock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  4.7 KB  |  306 lines

  1. /*
  2.  * testlock.c --
  3.  *    Locking test code.
  4.  */
  5.  
  6. #include <ctype.h>
  7. #include <stdio.h>
  8.  
  9. #include "c.h"
  10.  
  11. #include "catname.h"
  12. #include "lmgr.h"
  13. #include "heapam.h"
  14. #include "off.h"
  15. #include "pagenum.h"
  16. #include "xcxt.h"
  17.  
  18. RcsId("$Header: /private/postgres/src/test/RCS/testlock.c,v 1.3 1989/09/05 16:54:39 mao Version_2 $");
  19.  
  20. static bool        ProcessingTransaction;
  21. static Relation        StatisticRelation;
  22. static PagePartition    EightPartition;
  23.  
  24. /*
  25.  * DoProcess --
  26.  *    Perform main processing.
  27.  */
  28. static
  29. void
  30. DoProcess ARGS((
  31.     void
  32. ));
  33.  
  34. /*
  35.  * DoRelation --
  36.  *    Perform relation lock processing.
  37.  */
  38. static
  39. void
  40. DoRelation ARGS((
  41.     String    input
  42. ));
  43.  
  44. /*
  45.  * DoTuple --
  46.  *    Perform tuple level lock processing.
  47.  */
  48. static
  49. void
  50. DoTuple ARGS((
  51.     String    input
  52. ));
  53.  
  54. /*
  55.  * DoPage --
  56.  *    Perform page level lock processing.
  57.  */
  58. static
  59. void
  60. DoPage ARGS((
  61.     String    input
  62. ));
  63.  
  64. /*
  65.  * DoBye --
  66.  *    Perform normal cleanup and exit.
  67.  */
  68. static
  69. void
  70. DoBye ARGS((
  71.     void
  72. ));
  73.  
  74. /*
  75.  * DoError --
  76.  *    Perform error cleanup and exit.
  77.  */
  78. static
  79. void
  80. DoError ARGS((
  81.     void
  82. ));
  83.  
  84. TestMain()
  85. {
  86.     static int    beenHere = 0;
  87.  
  88.     ProcessingTransaction = false;
  89.     StatisticRelation = NULL;
  90.     EightPartition = CreatePagePartition(8192, 1024);
  91.  
  92.     if (beenHere == 0) {
  93.         beenHere = 1;
  94.     } else {
  95.         fputs("testlock: giving up!\n", stderr);
  96.         DoError();
  97.     }
  98.  
  99.     DoProcess();
  100.  
  101.     puts("testlock: finished!");
  102.  
  103.     DoBye();
  104. }
  105.  
  106. static
  107. void
  108. DoProcess()
  109. {
  110.     int            command;
  111.     int            mode;
  112.     LogicalPageNumber    pageNumber;
  113.     OffsetNumber        offsetNumber;
  114.     char            input[80];
  115.  
  116.     puts("Please enter a granularity/transaction [rtpSC], a mode [rwRW],");
  117.     puts("\ta page number, and a line number as appropriate.");
  118.  
  119.     for (;;) {
  120.         printf("(%c) ", (ProcessingTransaction) ? '+' : '-');
  121.  
  122.         if (scanf("%s", input) != 1) {
  123.             puts("No command!");
  124.             DoBye();
  125.         }
  126.  
  127.         command = input[0];
  128.  
  129.         switch (command) {
  130.         case 'r':
  131.             DoRelation(&input[1]);
  132.             break;
  133.         case 't':
  134.             DoTuple(&input[1]);
  135.             break;
  136.         case 'p':
  137.             DoPage(&input[1]);
  138.             break;
  139.         case 'S':
  140.             ProcessingTransaction = true;
  141.             StartTransactionCommand();
  142.             StatisticRelation = RelationNameOpenHeapRelation(
  143.                 StatisticRelationName);
  144.             break;
  145.         case 'C':
  146.             ProcessingTransaction = false;
  147.             CommitTransactionCommand();
  148.             break;
  149.         default:
  150.             fprintf(stderr, "testlock: Unknown command `%c'\n",
  151.                 command);
  152.             DoError();
  153.             break;
  154.         }
  155.     }
  156. }
  157.  
  158. static
  159. void
  160. DoRelation(input)
  161.     String    input;
  162. {
  163.     if (input[0] == '\0') {
  164.         if (scanf("%s", input) != 1) {
  165.             puts("No mode!");
  166.             DoBye();
  167.         }
  168.     }
  169.  
  170.     switch (input[0]) {
  171.     case 'r':
  172.         RelationSetLockForRead(StatisticRelation);
  173.         break;
  174.     case 'w':
  175.         RelationSetLockForWrite(StatisticRelation);
  176.         break;
  177.     default:
  178.         fprintf(stderr, "testlock: Unknown mode `%c'\n", input[0]);
  179.         DoError();
  180.         break;
  181.     }
  182. }
  183.  
  184. static
  185. void
  186. DoTuple(input)
  187.     String    input;
  188. {
  189.     LogicalPageNumber    pageNumber;
  190.     OffsetNumber        offsetNumber;
  191.     ItemPointerData        itemPointerData;
  192.  
  193.     if (input[0] == '\0') {
  194.         if (scanf("%s", input) != 1) {
  195.             puts("No mode!");
  196.             DoBye();
  197.         }
  198.     }
  199.     switch (input[0]) {
  200.     case 'r':
  201.         if (isdigit(input[1])) {
  202.             pageNumber = atoi(&input[1]);
  203.         } else {
  204.             if (scanf("%d", &pageNumber) != 1) {
  205.                 puts("No page [block]!");
  206.                 DoBye();
  207.             }
  208.         }
  209.         if (scanf("%hd", &offsetNumber) != 1) {
  210.             puts("No line [offset]!");
  211.             DoBye();
  212.         }
  213.  
  214.         ItemPointerSimpleSet(&itemPointerData, pageNumber,
  215.             offsetNumber);
  216.  
  217.         RelationSetLockForTupleRead(StatisticRelation,
  218.             &itemPointerData);
  219.         break;
  220.     default:
  221.         fprintf(stderr, "testlock: Unknown mode `%c'\n", input[0]);
  222.         DoError();
  223.         break;
  224.     }
  225. }
  226.  
  227. static
  228. void
  229. DoPage(input)
  230.     String    input;
  231. {
  232.     LogicalPageNumber    pageNumber;
  233.     OffsetNumber        offsetNumber;
  234.     ItemPointerData        itemPointerData;
  235.     int            mode;
  236.  
  237.     if (input[0] == '\0') {
  238.         if (scanf("%s", input) != 1) {
  239.             puts("No mode!");
  240.             DoBye();
  241.         }
  242.     }
  243.  
  244.     switch (input[0]) {
  245.     case 'r':
  246.     case 'w':
  247.     case 'R':
  248.     case 'W':
  249.         if (isdigit(input[1])) {
  250.             pageNumber = atoi(&input[1]);
  251.         } else {
  252.             if (scanf("%d", &pageNumber) != 1) {
  253.                 puts("No page!");
  254.                 DoBye();
  255.             }
  256.         }
  257.  
  258.         ItemPointerSetLogicalPageNumber(&itemPointerData,
  259.             EightPartition, pageNumber);
  260.         break;
  261.     default:
  262.         fprintf(stderr, "testlock: Unknown mode `%c'\n", input[0]);
  263.         DoError();
  264.         return;
  265.     }
  266.  
  267.     switch (input[0]) {
  268.     case 'r':
  269.         RelationSetLockForReadPage(StatisticRelation, EightPartition,
  270.             &itemPointerData);
  271.         break;
  272.     case 'w':
  273.         RelationSetLockForWritePage(StatisticRelation, EightPartition,
  274.             &itemPointerData);
  275.         break;
  276.     case 'R':
  277.         RelationUnsetLockForReadPage(StatisticRelation, EightPartition,
  278.             &itemPointerData);
  279.         break;
  280.     case 'W':
  281.         RelationUnsetLockForWritePage(StatisticRelation, EightPartition,
  282.             &itemPointerData);
  283.         break;
  284.     default:
  285.         fprintf(stderr, "testlock: internal error\n");
  286.         DoError();
  287.         break;
  288.     }
  289. }
  290.  
  291. static
  292. void
  293. DoBye()
  294. {
  295.     puts("Bye!");
  296.     StatusBackendExit(0);
  297. }
  298.  
  299. static
  300. void
  301. DoError()
  302. {
  303.     puts("Error!");
  304.     StatusBackendExit(1);
  305. }
  306.