Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2743d9a

Browse files
committed
Cosmetic improvements in ltree code.
Add more comments in ltree.h, and correct a misstatement or two.Use a symbol, rather than hardwired constants, for the maximum lengthof an ltree label. The max length is still hardwired in the associatederror messages, but I want to clean that up as part of a separate patchto improve the error messages.
1 parent122b0cc commit2743d9a

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

‎contrib/ltree/ltree.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@
77
#include"tsearch/ts_locale.h"
88
#include"utils/memutils.h"
99

10+
11+
/* ltree */
12+
13+
/*
14+
* We want the maximum length of a label to be encoding-independent, so
15+
* set it somewhat arbitrarily at 255 characters (not bytes), while using
16+
* uint16 fields to hold the byte length.
17+
*/
18+
#defineLTREE_LABEL_MAX_CHARS 255
19+
1020
typedefstruct
1121
{
12-
uint16len;
22+
uint16len;/* label string length in bytes */
1323
charname[FLEXIBLE_ARRAY_MEMBER];
1424
}ltree_level;
1525

@@ -19,7 +29,8 @@ typedef struct
1929
typedefstruct
2030
{
2131
int32vl_len_;/* varlena header (do not touch directly!) */
22-
uint16numlevel;
32+
uint16numlevel;/* number of labels */
33+
/* Array of maxalign'd ltree_level structs follows: */
2334
chardata[FLEXIBLE_ARRAY_MEMBER];
2435
}ltree;
2536

@@ -30,25 +41,35 @@ typedef struct
3041

3142
/* lquery */
3243

44+
/* lquery_variant: one branch of some OR'ed alternatives */
3345
typedefstruct
3446
{
35-
int32val;
36-
uint16len;
47+
int32val;/* CRC of label string */
48+
uint16len;/* label string length in bytes */
3749
uint8flag;/* see LVAR_xxx flags below */
3850
charname[FLEXIBLE_ARRAY_MEMBER];
3951
}lquery_variant;
4052

53+
/*
54+
* Note: these macros contain too many MAXALIGN calls and so will sometimes
55+
* overestimate the space needed for an lquery_variant. However, we can't
56+
* change it without breaking on-disk compatibility for lquery.
57+
*/
4158
#defineLVAR_HDRSIZE MAXALIGN(offsetof(lquery_variant, name))
4259
#defineLVAR_NEXT(x)( (lquery_variant*)( ((char*)(x)) + MAXALIGN(((lquery_variant*)(x))->len) + LVAR_HDRSIZE ) )
4360

44-
#defineLVAR_ANYEND 0x01
45-
#defineLVAR_INCASE 0x02
46-
#defineLVAR_SUBLEXEME0x04
61+
#defineLVAR_ANYEND 0x01/* '*' flag: prefix match */
62+
#defineLVAR_INCASE 0x02/* '@' flag: case-insensitive match */
63+
#defineLVAR_SUBLEXEME0x04/* '%' flag: word-wise match */
4764

65+
/*
66+
* In an lquery_level, "flag" contains the union of the variants' flags
67+
* along with possible LQL_xxx flags; so those bit sets can't overlap.
68+
*/
4869
typedefstruct
4970
{
5071
uint16totallen;/* total length of this level, in bytes */
51-
uint16flag;/* see LQL_xxxflags below */
72+
uint16flag;/* see LQL_xxxand LVAR_xxx flags */
5273
uint16numvar;/* number of variants; 0 means '*' */
5374
uint16low;/* minimum repeat count for '*' */
5475
uint16high;/* maximum repeat count for '*' */
@@ -60,7 +81,7 @@ typedef struct
6081
#defineLQL_NEXT(x) ( (lquery_level*)( ((char*)(x)) + MAXALIGN(((lquery_level*)(x))->totallen) ) )
6182
#defineLQL_FIRST(x)( (lquery_variant*)( ((char*)(x))+LQL_HDRSIZE ) )
6283

63-
#defineLQL_NOT0x10
84+
#defineLQL_NOT0x10/* level has '!' (NOT) prefix */
6485

6586
#ifdefLOWER_NODE
6687
#defineFLG_CANLOOKSIGN(x) ( ( (x) & ( LQL_NOT | LVAR_ANYEND | LVAR_SUBLEXEME ) ) == 0 )
@@ -73,7 +94,7 @@ typedef struct
7394
{
7495
int32vl_len_;/* varlena header (do not touch directly!) */
7596
uint16numlevel;/* number of lquery_levels */
76-
uint16firstgood;
97+
uint16firstgood;/* number of leading simple-match levels */
7798
uint16flag;/* see LQUERY_xxx flags below */
7899
/* Array of maxalign'd lquery_level structs follows: */
79100
chardata[FLEXIBLE_ARRAY_MEMBER];

‎contrib/ltree/ltree_io.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ ltree_in(PG_FUNCTION_ARGS)
8585
if (charlen==1&&t_iseq(ptr,'.'))
8686
{
8787
lptr->len=ptr-lptr->start;
88-
if (lptr->wlen>255)
88+
if (lptr->wlen>LTREE_LABEL_MAX_CHARS)
8989
ereport(ERROR,
9090
(errcode(ERRCODE_NAME_TOO_LONG),
9191
errmsg("name of level is too long"),
@@ -112,7 +112,7 @@ ltree_in(PG_FUNCTION_ARGS)
112112
if (state==LTPRS_WAITDELIM)
113113
{
114114
lptr->len=ptr-lptr->start;
115-
if (lptr->wlen>255)
115+
if (lptr->wlen>LTREE_LABEL_MAX_CHARS)
116116
ereport(ERROR,
117117
(errcode(ERRCODE_NAME_TOO_LONG),
118118
errmsg("name of level is too long"),
@@ -302,7 +302,7 @@ lquery_in(PG_FUNCTION_ARGS)
302302
((lptr->flag&LVAR_SUBLEXEME) ?1 :0)-
303303
((lptr->flag&LVAR_INCASE) ?1 :0)-
304304
((lptr->flag&LVAR_ANYEND) ?1 :0);
305-
if (lptr->wlen>255)
305+
if (lptr->wlen>LTREE_LABEL_MAX_CHARS)
306306
ereport(ERROR,
307307
(errcode(ERRCODE_NAME_TOO_LONG),
308308
errmsg("name of level is too long"),
@@ -318,7 +318,7 @@ lquery_in(PG_FUNCTION_ARGS)
318318
((lptr->flag&LVAR_SUBLEXEME) ?1 :0)-
319319
((lptr->flag&LVAR_INCASE) ?1 :0)-
320320
((lptr->flag&LVAR_ANYEND) ?1 :0);
321-
if (lptr->wlen>255)
321+
if (lptr->wlen>LTREE_LABEL_MAX_CHARS)
322322
ereport(ERROR,
323323
(errcode(ERRCODE_NAME_TOO_LONG),
324324
errmsg("name of level is too long"),
@@ -453,7 +453,7 @@ lquery_in(PG_FUNCTION_ARGS)
453453
errmsg("lquery syntax error"),
454454
errdetail("Unexpected end of line.")));
455455

456-
if (lptr->wlen>255)
456+
if (lptr->wlen>LTREE_LABEL_MAX_CHARS)
457457
ereport(ERROR,
458458
(errcode(ERRCODE_NAME_TOO_LONG),
459459
errmsg("name of level is too long"),
@@ -522,12 +522,21 @@ lquery_in(PG_FUNCTION_ARGS)
522522
}
523523
pfree(GETVAR(curqlevel));
524524
if (cur->numvar>1||cur->flag!=0)
525+
{
526+
/* Not a simple match */
525527
wasbad= true;
528+
}
526529
elseif (wasbad== false)
530+
{
531+
/* count leading simple matches */
527532
(result->firstgood)++;
533+
}
528534
}
529535
else
536+
{
537+
/* '*', so this isn't a simple match */
530538
wasbad= true;
539+
}
531540
curqlevel=NEXTLEV(curqlevel);
532541
cur=LQL_NEXT(cur);
533542
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp