This commit is contained in:
Oliver Lehmann 1989-04-11 11:18:52 +00:00
parent 53d3612c9c
commit a25959e1fd
3 changed files with 1521 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,234 @@
# include "gen.h"
# include "../h/dbs.h"
# include "../h/aux.h"
# include "../h/catalog.h"
# include "../h/access.h"
# include "../h/lock.h"
/*
** This is the DBU routine INDEX
**
** pc = # of parameters
** pv[0] points to primary relation name
** pv[1] points to index relation name
** pv[2] points to domain1
** pv[3] points to domain2
** .
** .
** .
** pv[pc] = -1
**
*/
struct dom
{
int id;
int off;
int frml;
char frm[5];
};
# ifdef STACK
static char primtup[MAXTUP];
static char systup[MAXTUP];
static DESC desc;
static DESC pridesc;
# endif
sec_index(pc, pv)
int pc;
char **pv;
{
register int i;
int j;
register struct dom *dom;
register char **p;
char *primary;
char *indx;
int ndoms;
int newpc;
struct tup_id tid;
struct tup_id hitid;
struct tup_id xtid;
char *newpv[MAXKEYS * 2 + 4];
# ifndef STACK
char primtup[MAXTUP];
char systup[MAXTUP];
DESC desc;
DESC pridesc;
# endif
extern DESC Reldes;
extern DESC Attdes;
extern DESC Inddes;
struct relation relkey;
struct relation reltup;
struct attribute attkey;
struct attribute atttup;
struct index indtup;
struct dom domain[MAXKEYS];
primary = pv[0];
indx = pv[1];
# ifdef xZTR1
AAtTfp(5, -1, "INDEX: (pri %s ind %s)\n", primary, indx);
# endif
i = AArelopen(&pridesc, 0, primary);
if (i == AMOPNVIEW_ERR)
return (error(5306, primary, (char *) 0));
if (i > 0)
return (error(5300, primary, (char *) 0));
if (i < 0)
AAsyserr(11040, primary, i);
if (!AAbequal(pridesc.reldum.relowner, AAusercode, 2))
{
i = 5303;
}
else if (pridesc.reldum.relstat & S_CATALOG)
{
i = 5305;
}
else if (ctoi(pridesc.reldum.relindxd) == SECINDEX)
{
i = 5304;
}
if (i)
{
AArelclose(&pridesc);
return (error(i, primary, (char *) 0));
}
/*
** GATHER INFO. ON DOMAINS
*/
opencatalog(AA_ATT, 2);
AAam_setkey(&Attdes, &attkey, primary, ATTRELID);
AAam_setkey(&Attdes, &attkey, pridesc.reldum.relowner, ATTOWNER);
pc -= 2;
p = &pv[2];
dom = domain;
for (i = 0; i < pc; i++)
{
if (i >= MAXKEYS)
{
AArelclose(&pridesc);
return (error(5301, *p, primary, (char *) 0)); /* too many keys */
}
AAam_setkey(&Attdes, &attkey, *p, ATTNAME);
j = AAgetequal(&Attdes, &attkey, &atttup, &tid);
if (j < 0)
AAsyserr(11041, j);
if (j)
{
AArelclose(&pridesc);
return (error(5302, *p, (char *) 0)); /* key not in relation */
}
dom->id = atttup.attid;
dom->off = atttup.attoff;
dom->frml = ctou(atttup.attfrml);
dom->frm[0] = atttup.attfrmt;
p++;
dom++;
}
ndoms = i;
AAnoclose(&Attdes);
/*
** The "order" of the steps have been altered to improve
** recovery possibilities
*/
/*
** STEP 1 & 2: CREATE INDEX RELATION.
*/
newpc = 0;
newpv[newpc++] = "0202"; /* S_NOUPDT | S_INDEX | */
newpv[newpc++] = indx;
p = &pv[2];
dom = domain;
for (i = 0; i < pc; i++)
{
newpv[newpc++] = *p;
AA_itoa(dom->frml, &dom->frm[1]);
newpv[newpc++] = dom->frm;
dom++;
p++;
}
newpv[newpc++] = "tidp";
newpv[newpc++] = "i4";
newpv[newpc] = (char *) -1;
# ifdef xZTR1
if (AAtTfp(5, 1, "INDEX: "))
AAprargs(newpc, newpv);
# endif
if (create(newpc, newpv))
return (-1);
/*
** STEP 5: FILL UP THE SECONDARY INDEX FILE ITSELF
*/
/* set a exclusive use lock */
if (AAsetusl(A_SLP, pridesc.reltid, M_EXCL) < 0)
goto tabort;
/* set a shared relation lock */
if (AAsetrll(A_SLP, pridesc.reltid, M_SHARE) < 0)
{
AAunlus(pridesc.reltid); /* release use lock */
tabort:
return (error(5000, (char *) 0));
}
if (i = AArelopen(&desc, 2, indx))
AAsyserr(11043, indx, i);
AAam_find(&pridesc, NOKEY, &tid, &hitid);
while (!(i = AAam_get(&pridesc, &tid, &hitid, primtup, TRUE)))
{
dom = domain;
for (i = j = 0; j < ndoms; j++)
{
AAbmove(&primtup[dom->off], &systup[i], dom->frml);
i += dom->frml;
dom++;
}
AAbmove(&tid, &systup[i], sizeof tid); /* move in pointer */
if ((j = AAam_insert(&desc, &xtid, systup, TRUE)) < 0)
AAsyserr(11044, indx, j);
}
if (i < 0)
AAsyserr(11045, primary, i);
AArelclose(&pridesc);
AArelclose(&desc);
/*
** STEP 3: ENTRIES TO INDEX-REL
*/
AApmove(primary, indtup.irelidp, MAXNAME, ' '); /* mv in primary name */
AAbmove(pridesc.reldum.relowner, indtup.iownerp, 2); /* primary owner */
AApmove(indx, indtup.irelidi, MAXNAME, ' '); /* index name */
indtup.irelspeci = M_HEAP;
for (i = 0; i < MAXKEYS; i++)
indtup.idom[i] = (i < ndoms)? domain[i].id: 0;
opencatalog(AA_INDEX, 2);
if ((i = AAam_insert(&Inddes, &tid, &indtup, TRUE)) < 0)
AAsyserr(11046, i);
/*
** STEP 4: TURN BIT ON IN PRIMARY RELATION TO SHOW IT IS BEING INDEXED
*/
opencatalog(AA_REL, 2);
AAam_setkey(&Reldes, &relkey, primary, RELID);
AAam_setkey(&Reldes, &relkey, pridesc.reldum.relowner, RELOWNER);
if (i = AAgetequal(&Reldes, &relkey, &reltup, &tid))
AAsyserr(11047, i);
reltup.relindxd = SECBASE;
if ((i = AAam_replace(&Reldes, &tid, &reltup, TRUE)) < 0)
AAsyserr(11048, i);
/*
** Flush all. This is necessary for recovery reasons.
*/
AApageflush((ACCBUF *) 0);
AAunlrl(pridesc.reltid); /* release relation lock */
AAunlus(pridesc.reltid); /* release use lock */
return (0);
}

View File

@ -0,0 +1,286 @@
# include <stdio.h>
# include "gen.h"
# include "../h/dbs.h"
# include "../h/catalog.h"
# include "../h/symbol.h"
# include "../h/lock.h"
# include "../h/pipes.h"
# include "../h/tree.h"
# include "qrymod.h"
/*
** DEFINE -- define various types of qrymod constraints
**
** This module takes care of defining all the various types of
** constraints that can be presented to qrymod. It makes sense
** out of them by looking at the funcid in the pipe. They
** correspond as follows:
**
** mdDEFINE -- a tree. This tree doesn't have any meaning yet; it is
** just the raw tree. It must be immediately followed
** by a type 1, 2, or three definition, which will link
** that tree into some other catalog, thereby giving
** it some sort of semantic meaning.
** mdVIEW -- a view. This block has the view name in it.
** mdPROT -- a protection constraint.
** mdINTEG -- an integrity constraint.
**
** Defines:
** define -- the driver: just calls one of the others.
** d_tree -- read definition tree.
** puttree -- write tree into AA_TREE catalog.
** relntrwr -- the physical write routine for puttree.
*/
QTREE *Treeroot;
extern struct pipfrmt Pipe;
/*
** DEFINE -- define driver
**
** This function does very little exciting; in fact, it just
** calls one of the other four functions to do the specific
** funcid functions.
**
** Parameters:
** func -- the function to perform:
** mdDEFINE -- define tree (used by all).
** mdVIEW -- define view.
** mdPROT -- define protection constraint.
** mdINTEG -- define integrity constraint.
*/
define(func)
register int func;
{
switch (func)
{
case mdDEFINE:
d_tree();
break;
case mdVIEW:
d_view();
break;
case mdPROT:
d_prot();
break;
case mdINTEG:
d_integ();
break;
default:
AAsyserr(19024, func);
}
}
/*
** D_TREE -- insert tree into system catalogs
**
** This routine reads in and saves a tree for further use.
** The root of this tree is saved in the global 'Treeroot'.
** The tree will ultimately be written to the AA_TREE catalog
** using the 'puttree' routine.
**
** Side Effects:
** The input pipe is read and the tree found there
** is built. A pointer to that tree is saved in
** 'Treeroot'. Notice that 'Qbuf' and the range
** table are clobbered.
*/
d_tree()
{
extern pipetrrd();
extern QTREE *readqry();
Treeroot = readqry(pipetrrd, TRUE);
rdpipe(P_SYNC, &Pipe, R_up);
# ifdef xQTR1
if (AAtTf(10, 0))
treepr(Treeroot, "Treeroot");
# endif
}
/*
** PUTTREE -- put tree into AA_TREE catalog
**
** The named tree is inserted into the AA_TREE catalog.
**
** The algorithm is to lock up the entire catalog and try to
** find the smallest unique id possible for the named relation.
**
** Parameters:
** root -- the root of the tree to insert.
** treerelid -- the relid of the relation for which
** this tree applies.
** treeowner -- the owner of the above relation.
** treetype -- the type of this tree; uses the mdXXX
** type (as mdPROT, mdINTEG, mdDISTR, etc.).
**
** Returns:
** The treeid that was assigned to this tree.
**
** Side Effects:
** The AA_TREE catalog gets locked, and information is
** inserted.
*/
puttree(root, treerelid, treeowner, treetype)
QTREE *root;
char *treerelid;
char *treeowner;
char treetype;
{
struct tree treekey;
struct tree treetup;
struct tup_id treetid;
register int i;
short treeid;
opencatalog(AA_TREE, 2);
AAam_clearkeys(&Treedes);
/*
** Find a unique tree identifier.
** Lock the AA_TREE catalog, and scan until we find a
** tuple which does not match.
*/
if (AAsetrll(A_SLP, Treedes.reltid, M_EXCL) < 0)
AAsyserr(5000);
AAam_setkey(&Treedes, &treekey, treerelid, TREERELID);
AAam_setkey(&Treedes, &treekey, treeowner, TREEOWNER);
AAam_setkey(&Treedes, &treekey, &treetype, TREETYPE);
for (treeid = 0; ; treeid++)
{
AAam_setkey(&Treedes, &treekey, &treeid, TREEID);
i = AAgetequal(&Treedes, &treekey, &treetup, &treetid);
# ifdef xQTR1
AAtTfp(10, 1, "PUTTREE: treeid %d AAgetequal %d\n", treeid, i);
# endif
if (i < 0)
AAsyserr(19025);
else if (i > 0)
break;
}
/*
** We have a unique tree id.
** Insert the new tuple and the tree into the
** AA_TREE catalog.
*/
relntrwr((char *) 0, 0, treerelid, treeowner, treetype, treeid);
writeqry(root, relntrwr);
relntrwr((char *) 0, 1);
/* all inserted -- flush pages and unlock */
if (AAnoclose(&Treedes))
AAsyserr(19026);
AAunlrl(Treedes.reltid);
return (treeid);
}
/*
** RELNTRWR -- physical tree write to relation
**
** This is the routine called from writeqry to write trees
** to the 'tree' relation (rather than the W_down pipe).
**
** It is assumed that the (treerelid, treeowner, treetype,
** treeid) combination is unique in the AA_TREE catalog, and that
** the AA_TREE catalog is locked.
**
** Parameters:
** ptr -- a pointer to the data. If NULL, this is
** a control call.
** len -- the length of the data. If ptr == NULL, this
** field is a control code: zero means
** initialize (thus taking the next two param-
** eters); one means flush.
** treerelid -- the name of the relation for which this
** tree applies (init only).
** treeowner -- the owner of this relation (init only).
** treetype -- on initialization, this tells what the
** tree is used for.
** treeid -- on initialization, this is the tree id we
** want to use.
**
** Returns:
** The number of bytes written ('len').
**
** Side Effects:
** Well, yes. Activity occurs in the AA_TREE catalog.
*/
relntrwr(ptr, len, treerelid, treeowner, treetype, treeid)
char *ptr;
int len;
char *treerelid;
char *treeowner;
int treetype;
int treeid;
{
static struct tree treetup;
struct tup_id treetid;
register char *p;
register int l;
static char *tptr;
p = ptr;
l = len;
/* check for special function */
if (!p)
{
switch (l)
{
case 0:
clr_tuple(&Treedes, &treetup);
AAbmove(treerelid, treetup.treerelid, MAXNAME);
AAbmove(treeowner, treetup.treeowner, 2);
treetup.treetype = treetype;
treetup.treeid = treeid;
tptr = treetup.treetree;
break;
case 1:
if (tptr != treetup.treetree)
{
if (AAam_insert(&Treedes, &treetid, &treetup, FALSE) < 0)
AAsyserr(19027);
}
break;
default:
AAsyserr(19028, l);
}
return;
}
/* output bytes */
while (l-- > 0)
{
*tptr++ = *p++;
/* check for buffer overflow */
if (tptr < &treetup.treetree[sizeof treetup.treetree])
continue;
/* yep, flush buffer to relation */
if (AAam_insert(&Treedes, &treetid, &treetup, FALSE) < 0)
AAsyserr(19029);
treetup.treeseq++;
tptr = treetup.treetree;
/* clear out the rest of the tuple for aesthetic reasons */
*tptr = ' ';
AAbmove(tptr, tptr + 1, sizeof treetup.treetree - 1);
}
return (len);
}