📄 ind_scan.c
字号:
/* * ind_scan.c - Index Control Programm * functions dealing with scanning of an index * Kernel of GNU SQL-server * * This file is a part of GNU SQL Server * * Copyright (c) 1996, 1997, Free Software Foundation, Inc * Developed at the Institute of System Programming * This file is written by Vera Ponomarenko * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Contacts: gss@ispras.ru * *//* $Id: ind_scan.c,v 1.245 1997/03/31 03:46:38 kml Exp $ */#include "xmem.h"#include "destrn.h"#include "sctp.h"#include "strml.h"#include "fdcltrn.h"extern struct des_field *d_f, *d_f2;extern u2_t seg_n;extern u2_t *afn;extern u2_t k_n;extern i4_t k2sz;static char *find_first_key (struct A *pg, u2_t pn){ char *asp = NULL, *a; u2_t cpn, ppn; struct ind_page *indph; while ((asp = getpg (pg, seg_n, pn, 's')) == NULL); indph = (struct ind_page *) asp; for (; indph->ind_wpage != LEAF;) { a = asp + indphsize + size2b; a += kszcal (a, afn, d_f) + k2sz; cpn = t2bunpack (a); putwul (pg, 'n'); ppn = pg->p_pn; while ((asp = getpg (pg, seg_n, cpn, 's')) == NULL); indph = (struct ind_page *) asp; BUF_unlock (seg_n, 1, &ppn); } return (asp);}#define BETWEEN_CMP (t == SS || t == SES || t == SSE || t == SESE)static intcmp_with_dia (char *key, char *diasc, char *diaval){ char *kval, *keyval; i4_t sst, k1, ftype, v, v1; char *a1, *a2; u2_t k, nk, n1, n2; unsigned char t; sst = 1; kval = keyval = key + scscal (key); t = selsc1 (&diasc, sst++); v = v1 = 0; for (k = 0, k1 = 0; t != ENDSC && k < k_n && key < keyval; k++, k1++) { if (k1 == 7) { k1 = 0; key++; } if ((*key & BITVL(k1)) != 0) { if (t == EQUN) return (1); ftype = (d_f + afn[k])->field_type; if (t == NEQUN || t == ANY) kval = proval (kval, ftype); else { switch (ftype) { case T1B: diaval = ftint (t, diaval, &a1, &a2, size1b); v = f1b (kval, a1, size1b, size1b); if (BETWEEN_CMP) v1 = f1b (kval, a2, size1b, size1b); kval++; break; case T2B: diaval = ftint (t, diaval, &a1, &a2, size2b); v = f2b (kval, a1, size2b, size2b); if (BETWEEN_CMP) v1 = f2b (kval, a2, size2b, size2b); kval += size2b; break; case T4B: diaval = ftint (t, diaval, &a1, &a2, size4b); v = f4b (kval, a1, size4b, size4b); if (BETWEEN_CMP) v1 = f4b (kval, a2, size4b, size4b); kval += size4b; break; case TFLOAT: diaval = ftint (t, diaval, &a1, &a2, size4b); v = flcmp (kval, a1, size4b, size4b); if (BETWEEN_CMP) v1 = flcmp (kval, a2, size4b, size4b); kval += size4b; break; case TFL: nk = t2bunpack (kval); kval += size2b; diaval = ftch (t, diaval, &a1, &a2, &n1, &n2); v = ffloat (kval, a1, nk, n1); if (BETWEEN_CMP) v1 = ffloat (kval, a2, nk, n2); kval += nk; break; case TCH: nk = t2bunpack (kval); kval += size2b; diaval = ftch (t, diaval, &a1, &a2, &n1, &n2); v = chcmp (kval, a1, nk, n1); if (BETWEEN_CMP) v1 = chcmp (kval, a2, nk, n2); kval += nk; break; default: break; } if (v == 0) /* current == first */ { if (t == SML) return (EOI); if (t == NEQ || t == GRT || t == SS || t == SSE) return (1); } else if (v > 0) /* current > first */ { if (t == EQ || t == SML || t == SMLEQ) return (EOI); if (BETWEEN_CMP) if (v1 == 0) /* current == last */ if (t == SS || t == SES) return (EOI); else {} else if (v1 > 0) /* current > last */ return (EOI); } else /* current < first */ { if (!(t == NEQ || t == SML || t == SMLEQ)) return (1); } } } else { /* values are absent into the key */ if (!(t == EQUN || t == ANY)) return (EOI); } t = selsc1 (&diasc, sst++); } if (key == keyval && t != ENDSC) return (EOI); return (OK);}#undef BETWEEN_CMPstatic char *find_next_agr (struct A *pg, char *diasc, char *diaval, i4_t elsz, char *key, i4_t *agrloc){ char *lastb, *agr, *asp = NULL; struct ind_page *indph; u2_t pn, ppn, n, off; i4_t keysz, l; asp = pg->p_shm; pn = pg->p_pn; indph = (struct ind_page *) asp; for (;;) { off = indph->ind_off; lastb = asp + off; for (; key < lastb; key += keysz + n * elsz) { agr = key; n = t2bunpack (key); key += size2b; keysz = kszcal (key, afn, d_f); if (diasc == NULL) { *agrloc = agr - asp; return (key + keysz); } if ((l = cmp_with_dia (key, diasc, diaval)) == EOI) return (NULL); if (l == OK) { *agrloc = agr - asp; return (key + keysz); } } pn = indph->ind_nextpn; if (pn == (u2_t) ~ 0) return (NULL); putwul (pg, 'n'); ppn = pg->p_pn; while ((asp = getpg (pg, seg_n, pn, 's')) == NULL); indph = (struct ind_page *) asp; BUF_unlock (seg_n, 1, &ppn); key = asp + indphsize; }}static char *first_key_frm (char *diasc, char *diaval, char *mas){ char *a; i4_t ftype; u2_t size; unsigned char t; t = selsc1 (&diasc, 1); if (t == EQ || t == GRT || t == GRTEQ || t == SS || t == SES || t == SSE || t == SESE) { a = mas; ftype = (d_f + afn[0])->field_type; if (ftype == TCH || ftype == TFL) { size = t2bunpack (diaval); diaval += size2b; t2bpack (size, a); a += size2b; } else size = (d_f + afn[0])->field_size; bcopy (diaval, a, size); } else mas = NULL; return (mas);}static intcmpfval (char *key, char *aval) /* key - in the index, aval - from diasc */{ char *kval; i4_t ftype, v = 0; u2_t nk, n1; kval = key + scscal (key); if ((*key & BITVL(0)) != 0) { ftype = (d_f + afn[0])->field_type; switch (ftype) { case T1B: v = f1b (kval, aval, size1b, size1b); break; case T2B: v = f2b (kval, aval, size2b, size2b); break; case T4B: v = f4b (kval, aval, size4b, size4b); break; case TFLOAT: v = flcmp (kval, aval, size4b, size4b); break; case TFL: nk = t2bunpack (kval); kval += size2b; n1 = t2bunpack (aval); aval += size2b; v = ffloat (kval, aval, nk, n1); break; case TCH: nk = t2bunpack (kval); kval += size2b; n1 = t2bunpack (aval); aval += size2b; v = chcmp (kval, aval, nk, n1); break; default: break; } } else v = 1; return (v);}static char *flookup (struct A *pg, u2_t pn, char *key, i4_t infsz, char **rasp){ char *asp = NULL, *a, *lastb; u2_t ppn, off; i4_t elsz; u2_t n; struct ind_page *indph; while ((asp = getpg (pg, seg_n, pn, 's')) == NULL); indph = (struct ind_page *) asp; for (; indph->ind_wpage != LEAF;) { off = indph->ind_off; lastb = asp + off; elsz = k2sz + size2b; for (a = asp + indphsize; a < lastb; a += kszcal (a, afn, d_f) + n * elsz) { n = t2bunpack (a); a += size2b; if (cmpfval (a, key) >= 0) { a += kszcal (a, afn, d_f) + k2sz; break; } } if (a == lastb) { *rasp = asp; return (a); } pn = t2bunpack (a); a += size2b; putwul (pg, 'n'); ppn = pg->p_pn; while ((asp = getpg (pg, seg_n, pn, 's')) == NULL); indph = (struct ind_page *) asp; BUF_unlock (seg_n, 1, &ppn); } elsz = k2sz + infsz; off = indph->ind_off; lastb = asp + off; for (a = asp + indphsize; a < lastb; a += kszcal (a, afn, d_f) + n * elsz) { n = t2bunpack (a); a += size2b; if (cmpfval (a, key) >= 0) { a -= size2b; break; } } *rasp = asp; return (a);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -