File: task_cursor.adb

package info (click to toggle)
libgnatcoll 1.7gpl2015-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,280 kB
  • ctags: 1,124
  • sloc: ada: 134,072; python: 4,017; cpp: 1,397; ansic: 1,234; makefile: 368; sh: 152; xml: 31; sql: 6
file content (188 lines) | stat: -rw-r--r-- 5,070 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
with Ada.Exceptions;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO;           use Ada.Text_IO;
with Ada.Real_Time;         use Ada.Real_Time;

with Ada.Synchronous_Barriers;

with GNATCOLL.Atomic;       use GNATCOLL.Atomic;
with GNATCOLL.SQL;          use GNATCOLL.SQL;
with GNATCOLL.SQL.Exec;     use GNATCOLL.SQL.Exec;
with GNATCOLL.SQL.Exec.Tasking;
with GNATCOLL.Traces;       use GNATCOLL.Traces;

procedure Task_Cursor (Descr : Database_Description; Speed : Boolean) is
   use type Atomic_Counter;

   DB     : Database_Connection;
   Insert : Prepared_Statement :=
     Prepare ("insert into Test_Task_Cursor values ($1, $2, $3, $4)");
   Query  : array (Boolean) of Prepared_Statement;
   Sample : array (Boolean) of Unbounded_String;

   Stamp : Time;

   package ASB renames Ada.Synchronous_Barriers;

   B : ASB.Synchronous_Barrier (7);
   Printed : aliased Atomic_Counter := 0;
   Switch  : aliased Atomic_Counter := 0;

   procedure Fetch_Query
     (DB : Database_Connection;
      Query : Prepared_Statement;
      Output : out Unbounded_String);

   task type Parallel;

   --------------
   -- Parallel --
   --------------

   task body Parallel is
      Dummy : Boolean;
      DB    : Database_Connection;
      Idx   : constant Boolean :=
        Sync_Add_And_Fetch (Switch'Access, 1) rem 2 = 0;

      procedure Process (Idx : Boolean);

      -------------
      -- Process --
      -------------

      procedure Process (Idx : Boolean) is
         Data  : Unbounded_String;
      begin
         Fetch_Query (DB, Query (Idx), Data);

         if Data /= Sample (Idx) then
            if Sync_Add_And_Fetch (Printed'Access, 1) = 1 then
               Put_Line
               ("Differ" & Length (Sample (Idx))'Img & Length (Data)'Img
                  & ASCII.LF & To_String (Sample (Idx)) & ASCII.LF
                  & To_String (Data) & ASCII.LF
                  & "-------------------------------------------------------");
            end if;
         end if;
      end Process;

   begin
      ASB.Wait_For_Release (B, Dummy);
      DB := Tasking.Get_Task_Connection (Descr);

      Process (Idx);
      Process (not Idx);

      ASB.Wait_For_Release (B, Dummy);

   exception
      when E : others =>
         Put_Line ("Task " & Ada.Exceptions.Exception_Information (E));
         ASB.Wait_For_Release (B, Dummy);
   end Parallel;

   -----------------
   -- Fetch_Query --
   -----------------

   procedure Fetch_Query
     (DB : Database_Connection;
      Query : Prepared_Statement;
      Output : out Unbounded_String)
   is
      Result : Forward_Cursor;
      Do_Output : Boolean := not Speed or else Output = Null_Unbounded_String;
   begin
      if Do_Output then
         Output := Null_Unbounded_String;
      end if;

      Result.Fetch (DB, Query);

      if Do_Output then
         while Result.Has_Row loop
            for J in 0 .. Result.Field_Count - 1 loop
               Append
                 (Output, To_Unbounded_String (ASCII.HT & Result.Value (J)));
            end loop;

            Append
              (Output,
               Integer'Image (Result.Processed_Rows)
               & Integer'Image (Result.Current)
               & ASCII.HT & Boolean'Image (Result.Boolean_Value (3))
               & ASCII.LF);

            Result.Next;
         end loop;
      end if;

   end Fetch_Query;

   Test_Tasks : array (1 .. B.Release_Threshold - 1) of Parallel;

begin
   GNATCOLL.Traces.Parse_Config_File;
   DB := Descr.Build_Connection;

   Query (False) :=
     Prepare ("select * from Test_Task_Cursor order by 1 desc",
              Use_Cache => True, Name => "statement_desc");
   Query (True) :=
     Prepare ("select F_Text, F_Real, F_Int, F_Bool from Test_Task_Cursor"
              & " order by 1",
              Use_Cache => True, Name => "statement_asc");

   if not Check_Connection (DB) then
      Ada.Text_IO.Put_Line ("Check_Connection should return True");
   end if;

   DB.Execute
     ("create table Test_Task_Cursor"
      & " (F_Int Int, F_Real Real, F_Text Text, F_Bool Boolean)");

   if not DB.Success then
      --  Could be already exists
      DB.Rollback;
   end if;

   for J in 0 .. 9 loop
      DB.Execute
        (Insert,
         (1 => +J,
          2 => +(Float (J) * 10.0),
          3 => +(Float (J) + Float (J) * 1000.0),
          4 => +(J rem 2 = 0)));
   end loop;

   DB.Commit;

   Stamp := Clock;

   for J in 1 .. (if Speed then 1000_000 else 1) loop
      Fetch_Query (DB, Query (False), Sample (False));
      Fetch_Query (DB, Query (True), Sample (True));
   end loop;

   if Speed then
      Put_Line ("Time spend" & Duration'Image (To_Duration (Clock - Stamp)));
   end if;

   declare
      Dummy : Boolean;
   begin
      ASB.Wait_For_Release (B, Dummy);
      ASB.Wait_For_Release (B, Dummy);
   end;

   if Printed = 0 then
      Put_Line ("All equals");
      Put_Line (To_String (Sample (False)));
      Put_Line (To_String (Sample (True)));
   end if;

   DB.Execute ("drop table Test_Task_Cursor");

   DB.Commit;
end Task_Cursor;