File: RingoShadowFetch.cs

package info (click to toggle)
indigo 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,936 kB
  • sloc: ansic: 332,816; cpp: 169,470; python: 20,033; java: 13,701; cs: 9,979; asm: 8,475; sql: 6,743; xml: 6,354; javascript: 1,245; sh: 555; php: 506; makefile: 54
file content (113 lines) | stat: -rw-r--r-- 3,533 bytes parent folder | download | duplicates (6)
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
using System.IO;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Globalization;

namespace indigo
{
   public class RingoShadowFetch
   {
      RingoIndexData _index_data;
      enum SearchType
      {
         UNDEF, EXACT
      };
      SearchType search_type = SearchType.UNDEF;
      string where_clause;

      public int? nextAfterStorageId { get; set; }

      public RingoShadowFetch (RingoIndexData index_data)
      {
         _index_data = index_data;
      }

      public void prepareExact (string query, string options)
      {
         int res = BingoCore.lib.ringoSetupMatch("REXACT", query, options);
         if (res < 0)
            throw new Exception(BingoCore.lib.bingoGetError());

         int query_hash;
         BingoCore.lib.ringoGetHash(false, out query_hash);
         where_clause = String.Format("hash = {0}", query_hash);
         search_type = SearchType.EXACT;
      }

      public IEnumerable<FetchedData> fetch (SqlConnection conn)
      {                      
         ArrayList res_list = new ArrayList();

         StringBuilder command_text = new StringBuilder();
         command_text.Append("SELECT sh.id");
         if (search_type == SearchType.EXACT)
         {
            command_text.Append(", sh.crf");
         }
         command_text.AppendFormat(" FROM {0} sh", _index_data.shadowTable);

         StringBuilder final_where_clause = new StringBuilder();
         if (where_clause.Length > 0)
         {
            final_where_clause.Append(" ( ");
            final_where_clause.Append(where_clause);
            final_where_clause.Append(" ) ");
         }
         if (nextAfterStorageId != null)
         {
            if (final_where_clause.Length > 0)
               final_where_clause.Append(" and ");
            final_where_clause.Append(" (");
            final_where_clause.AppendFormat(" storage_id > {0} ", nextAfterStorageId.Value);
            final_where_clause.Append(" )");
         }
         if (final_where_clause.Length > 0)
         {
            command_text.Append(" WHERE ");
            command_text.Append(final_where_clause.ToString());
         }

         if (nextAfterStorageId.HasValue)
            command_text.Append(" ORDER BY storage_id");

         UTF8Encoding encoding = new UTF8Encoding();

         using (SqlCommand cmd = new SqlCommand(command_text.ToString(), conn))
         {
            cmd.CommandTimeout = 3600;
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
               while (reader.Read())
               {
                  int id = Convert.ToInt32(reader[0]);

                  int res;
                  if (search_type == SearchType.EXACT)
                  {
                     byte[] crf = (byte[])reader[1];
                     res = BingoCore.lib.ringoMatchTargetBinary(crf, crf.Length);
                  }
                  else
                     throw new Exception("Search type is undefined");

                  if (res == -2)
                     throw new Exception(BingoCore.lib.bingoGetError());
                  if (res == -1)
                     throw new Exception(BingoCore.lib.bingoGetWarning());

                  if (res == 1)
                  {
                     FetchedData data = new FetchedData(id);
                     yield return data;
                  }
               }
            }
         }
      }
   }
}