forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTsVector.cs
More file actions
502 lines (450 loc) · 16.7 KB
/
NpgsqlTsVector.cs
File metadata and controls
502 lines (450 loc) · 16.7 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
namespace NpgsqlTypes
{
/// <summary>
/// Represents a PostgreSQL tsvector.
/// </summary>
public class NpgsqlTsVector : IEnumerable<NpgsqlTsVector.Lexeme>
{
List<Lexeme> _lexemes;
internal NpgsqlTsVector(List<Lexeme> lexemes, bool noCheck)
{
if (noCheck)
_lexemes = lexemes;
else
Load(lexemes);
}
/// <summary>
/// Constructs an NpgsqlTsVector from a list of lexemes. This also sorts and remove duplicates.
/// </summary>
/// <param name="lexemes"></param>
public NpgsqlTsVector(List<Lexeme> lexemes)
{
Load(lexemes);
}
void Load(List<Lexeme> lexemes)
{
_lexemes = new List<Lexeme>(lexemes);
if (_lexemes.Count == 0)
return;
// Culture-specific comparisons doesn't really matter for the backend. It's sorting on its own if it detects an unsorted collection.
// Only when a .NET user wants to print the sort order.
_lexemes.Sort((a, b) => a.Text.CompareTo(b.Text));
int res = 0;
int pos = 1;
while (pos < _lexemes.Count)
{
if (_lexemes[pos].Text != _lexemes[res].Text)
{
// We're done with this lexeme. First make sure the word pos list is sorted and contains unique elements.
_lexemes[res] = new Lexeme(_lexemes[res].Text, Lexeme.UniquePos(_lexemes[res]._wordEntryPositions), true);
res++;
if (res != pos)
_lexemes[res] = _lexemes[pos];
}
else
{
// Just concatenate the word pos lists
if (_lexemes[res]._wordEntryPositions != null)
{
if (_lexemes[pos].Count > 0)
_lexemes[res]._wordEntryPositions.AddRange(_lexemes[pos]._wordEntryPositions);
}
else
{
_lexemes[res] = _lexemes[pos];
}
}
pos++;
}
// Last element
_lexemes[res] = new Lexeme(_lexemes[res].Text, Lexeme.UniquePos(_lexemes[res]._wordEntryPositions), true);
if (res != pos - 1)
{
_lexemes.RemoveRange(res, pos - 1 - res);
}
}
/// <summary>
/// Parses a tsvector in PostgreSQL's text format.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static NpgsqlTsVector Parse(string value)
{
if (value == null)
throw new ArgumentNullException("value");
Contract.EndContractBlock();
List<Lexeme> lexemes = new List<Lexeme>();
int pos = 0;
int wordPos = 0;
StringBuilder sb = new StringBuilder();
List<Lexeme.WordEntryPos> wordEntryPositions;
WaitWord:
if (pos >= value.Length)
goto Finish;
if (Char.IsWhiteSpace(value[pos]))
{
pos++;
goto WaitWord;
}
sb.Clear();
if (value[pos] == '\'')
{
pos++;
goto WaitEndComplex;
}
if (value[pos] == '\\')
{
pos++;
goto WaitNextChar;
}
sb.Append(value[pos++]);
goto WaitEndWord;
WaitNextChar:
if (pos >= value.Length)
throw new FormatException("Missing escaped character after \\ at end of value");
sb.Append(value[pos++]);
goto WaitEndWord;
WaitEndWord:
if (pos >= value.Length || Char.IsWhiteSpace(value[pos]))
{
lexemes.Add(new Lexeme(sb.ToString()));
if (pos >= value.Length)
goto Finish;
pos++;
goto WaitWord;
}
if (value[pos] == '\\')
{
pos++;
goto WaitNextChar;
}
if (value[pos] == ':')
{
pos++;
goto StartPosInfo;
}
sb.Append(value[pos++]);
goto WaitEndWord;
WaitEndComplex:
if (pos >= value.Length)
throw new FormatException("Unexpected end of value");
if (value[pos] == '\'')
{
pos++;
goto WaitCharComplex;
}
if (value[pos] == '\\')
{
pos++;
if (pos >= value.Length)
throw new FormatException("Missing escaped character after \\ at end of value");
}
sb.Append(value[pos++]);
goto WaitEndComplex;
WaitCharComplex:
if (pos < value.Length && value[pos] == '\'')
{
sb.Append('\'');
pos++;
goto WaitEndComplex;
}
if (pos < value.Length && value[pos] == ':')
{
pos++;
goto StartPosInfo;
}
lexemes.Add(new Lexeme(sb.ToString()));
goto WaitWord;
StartPosInfo:
wordEntryPositions = new List<Lexeme.WordEntryPos>();
goto InPosInfo;
InPosInfo:
int digitPos = pos;
while (pos < value.Length && value[pos] >= '0' && value[pos] <= '9')
pos++;
if (digitPos == pos)
throw new FormatException("Missing length after :");
wordPos = int.Parse(value.Substring(digitPos, pos - digitPos));
goto WaitPosWeightOrDelim;
// Note: PostgreSQL backend parser matches also for example 1DD2A, which is parsed into 1A, but not 1AA2D ...
WaitPosWeightOrDelim:
if (pos < value.Length)
{
if (value[pos] == 'A' || value[pos] == 'a' || value[pos] == '*') // Why * ?
{
wordEntryPositions.Add(new Lexeme.WordEntryPos(wordPos, Lexeme.Weight.A));
pos++;
goto WaitPosDelim;
}
if (value[pos] >= 'B' && value[pos] <= 'D' || value[pos] >= 'b' && value[pos] <= 'd')
{
char weight = value[pos];
if (weight >= 'b' && weight <= 'd')
weight = (char)(weight - ('b' - 'B'));
wordEntryPositions.Add(new Lexeme.WordEntryPos(wordPos, Lexeme.Weight.D + ('D' - weight)));
pos++;
goto WaitPosDelim;
}
}
wordEntryPositions.Add(new Lexeme.WordEntryPos(wordPos));
goto WaitPosDelim;
WaitPosDelim:
if (pos >= value.Length || Char.IsWhiteSpace(value[pos]))
{
if (pos < value.Length)
pos++;
lexemes.Add(new Lexeme(sb.ToString(), wordEntryPositions));
goto WaitWord;
}
if (value[pos] == ',')
{
pos++;
goto InPosInfo;
}
throw new FormatException("Missing comma, whitespace or end of value after lexeme pos info");
Finish:
return new NpgsqlTsVector(lexemes);
}
/// <summary>
/// Returns the lexeme at a specific index
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public Lexeme this[int index]
{
get
{
if (index < 0 || index >= _lexemes.Count)
throw new ArgumentOutOfRangeException("index");
return _lexemes[index];
}
}
/// <summary>
/// Gets the number of lexemes.
/// </summary>
public int Count { get { return _lexemes.Count; } }
/// <summary>
/// Returns an enumerator.
/// </summary>
/// <returns></returns>
public IEnumerator<Lexeme> GetEnumerator()
{
return _lexemes.GetEnumerator();
}
/// <summary>
/// Returns an enumerator.
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return _lexemes.GetEnumerator();
}
/// <summary>
/// Gets a string representation in PostgreSQL's format.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Join(" ", _lexemes);
}
/// <summary>
/// Represents a lexeme. A lexeme consists of a text string and optional word entry positions.
/// </summary>
public struct Lexeme
{
string _text;
internal List<WordEntryPos> _wordEntryPositions;
/// <summary>
/// Creates a lexeme with no word entry positions.
/// </summary>
/// <param name="text"></param>
public Lexeme(string text)
{
_text = text;
_wordEntryPositions = null;
}
/// <summary>
/// Creates a lexeme with word entry positions.
/// </summary>
/// <param name="text"></param>
/// <param name="wordEntryPositions"></param>
public Lexeme(string text, List<WordEntryPos> wordEntryPositions)
{
_text = text;
if (wordEntryPositions != null)
_wordEntryPositions = new List<WordEntryPos>(wordEntryPositions);
else
_wordEntryPositions = null;
}
internal Lexeme(string text, List<WordEntryPos> wordEntryPositions, bool noCopy)
{
_text = text;
if (wordEntryPositions != null)
_wordEntryPositions = noCopy ? wordEntryPositions : new List<WordEntryPos>(wordEntryPositions);
else
_wordEntryPositions = null;
}
internal static List<WordEntryPos> UniquePos(List<WordEntryPos> list)
{
if (list == null)
return null;
bool needsProcessing = false;
for (var i = 1; i < list.Count; i++)
{
if (list[i - 1].Pos >= list[i].Pos)
{
needsProcessing = true;
break;
}
}
if (!needsProcessing)
return list;
// Don't change the original list, as the user might inspect it later if he holds a reference to the lexeme's list
list = new List<WordEntryPos>(list);
list.Sort((x, y) => x.Pos.CompareTo(y.Pos));
int a = 0;
for (int b = 1; b < list.Count; b++)
{
if (list[a].Pos != list[b].Pos)
{
a++;
if (a != b)
list[a] = list[b];
}
else if (list[b].Weight > list[a].Weight)
list[a] = list[b];
}
if (a != list.Count - 1)
{
list.RemoveRange(a, list.Count - 1 - a);
}
return list;
}
/// <summary>
/// Gets or sets the text.
/// </summary>
public string Text { get { return _text ?? ""; } set { _text = value; } }
/// <summary>
/// Gets a word entry position.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public WordEntryPos this[int index]
{
get
{
if (index < 0 || _wordEntryPositions == null || index >= _wordEntryPositions.Count)
throw new ArgumentOutOfRangeException("index");
return _wordEntryPositions[index];
}
internal set
{
if (index < 0 || _wordEntryPositions == null || index >= _wordEntryPositions.Count)
throw new ArgumentOutOfRangeException("index");
_wordEntryPositions[index] = value;
}
}
/// <summary>
/// Gets the number of word entry positions.
/// </summary>
public int Count { get { return _wordEntryPositions == null ? 0 : _wordEntryPositions.Count; } }
/// <summary>
/// Creates a string representation in PostgreSQL's format.
/// </summary>
/// <returns></returns>
public override string ToString()
{
var str = '\'' + (_text ?? "").Replace(@"\", @"\\").Replace("'", "''") + '\'';
if (Count > 0)
str += ":" + string.Join(",", _wordEntryPositions);
return str;
}
/// <summary>
/// Represents a word entry position and an optional weight.
/// </summary>
public struct WordEntryPos
{
internal short _val;
internal WordEntryPos(short value)
{
_val = value;
}
/// <summary>
/// Creates a WordEntryPos with a given position and weight.
/// </summary>
/// <param name="pos">Position values can range from 1 to 16383; larger numbers are silently set to 16383.</param>
/// <param name="weight">A weight labeled between A and D.</param>
public WordEntryPos(int pos, Weight weight = Weight.D)
{
if (pos == 0)
throw new ArgumentOutOfRangeException("pos", "Lexeme position is out of range. Min value is 1, max value is 2^14-1. Value was: " + pos);
if (weight < Weight.D || weight > Weight.A)
throw new ArgumentOutOfRangeException("weight");
Contract.EndContractBlock();
// Per documentation: "Position values can range from 1 to 16383; larger numbers are silently set to 16383."
if ((pos >> 14) != 0)
pos = (1 << 14) - 1;
_val = (short)(((int)weight << 14) | pos);
}
/// <summary>
/// The weight is labeled from A to D. D is the default, and not printed.
/// </summary>
public Weight Weight
{
get
{
return (Weight)((_val >> 14) & 3);
}
}
/// <summary>
/// The position is a 14-bit unsigned integer indicating the position in the text this lexeme occurs. Cannot be 0.
/// </summary>
public int Pos
{
get
{
return _val & ((1 << 14) - 1);
}
}
/// <summary>
/// Prints this lexeme in PostgreSQL's format, i.e. position is followed by weight (weight is only printed if A, B or C).
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (Weight != Weight.D)
return Pos + Weight.ToString();
return Pos.ToString();
}
}
/// <summary>
/// The weight is labeled from A to D. D is the default, and not printed.
/// </summary>
public enum Weight
{
/// <summary>
/// D, the default
/// </summary>
D = 0,
/// <summary>
/// C
/// </summary>
C = 1,
/// <summary>
/// B
/// </summary>
B = 2,
/// <summary>
/// A
/// </summary>
A = 3
}
}
}
}