DBCompare.mesa, Grapevine: database comparison tool
Hal Murray May 1, 1985 8:05:57 pm PDT
Hal Murray, March 13, 1986 5:32:38 pm PST
HGM May 31, 1984 3:19:54 am PDT
Andrew Birrell February 8, 1984 4:42 pm
Mike Schroeder March 19, 1983 2:12 pm
DIRECTORY
BasicTime USING [FromPupTime],
GVBasics,
Commander USING [CommandProc, Register],
GVLocate USING [FindRegServer, FoundServerInfo],
GVNames,
GVProtocol,
IO,
Process USING [Detach],
Pup USING [Address, nullSocket],
PupName USING [NameLookup],
Rope USING [Cat, Find, ROPE, Substr],
ViewerIO USING [CreateViewerStreams];
DBCompare: MONITOR
IMPORTS BasicTime, GVLocate, GVNames, GVProtocol, IO, Process, PupName, Rope, Commander, ViewerIO =
BEGIN
OPEN GVBasics, IO, GVNames, GVProtocol;
targetRope: Rope.ROPE; -- server under investigation --
targetAddr: Pup.Address; -- target's address --
targetStr: Handle ← NIL; -- stream to target server --
serverAddr: Pup.Address; -- other server's address --
addrKnown: BOOLEANFALSE; -- validity of serverAddr --
str: Handle ← NIL; -- stream to other server --
ReadElsewhere: SAFE PROC [out: IO.STREAM, name: GVBasics.RName]
RETURNS [rc: GVProtocol.ReturnCode] = CHECKED
BEGIN
OPEN GVProtocol;
TryUpdate: SAFE PROC[str: GVProtocol.Handle] = CHECKED
BEGIN
SendRSOperation[str, ReadEntry];
SendRName[str, name];
SendTimestamp[str, GVBasics.oldestTime];
SendNow[str];
rc ← ReceiveRC[str];
END;
oldBad: BOOLEANFALSE;
Create: SAFE PROC = CHECKED
BEGIN
out.PutF["%b#%b#", [cardinal[serverAddr.net]], [cardinal[serverAddr.host]] ];
str ← GVProtocol.CreateStream[serverAddr, RSEnquiry];
END;
Destroy: SAFE PROC = CHECKED
BEGIN
IF str # NIL THEN str.Close[];
str ← NIL;
END;
Accept: SAFE PROC [addr: Pup.Address] RETURNS [BOOL] = CHECKED
BEGIN
IF addr.net = targetAddr.net AND addr.host = targetAddr.host THEN RETURN[FALSE];
IF str # NIL AND serverAddr # addr THEN Destroy[];
IF str = NIL
THEN BEGIN
serverAddr ← addr;
Create[ ! Failed => GOTO failed];
addrKnown ← TRUE;
END;
RETURN[TRUE];
EXITS failed => RETURN[FALSE]
END;
BEGIN
IF str # NIL
THEN BEGIN
TryUpdate[ str ! Failed => GOTO streamGone ];
EXITS streamGone => Destroy[];
END;
IF str = NIL
THEN BEGIN
IF addrKnown
THEN Create[ ! Failed => GOTO notThere]
ELSE BEGIN
[] ← GVLocate.FindRegServer["x.GV", Accept];
IF str = NIL THEN GOTO notThere;
END;
TryUpdate[str ! Failed => GOTO notThere];
END;
IF rc.code = WrongServer
THEN oldBad ← TRUE
ELSE oldBad ← FALSE;
EXITS notThere => { Destroy[]; oldBad ← TRUE };
END;
IF oldBad
THEN BEGIN -- need to find the correct R-Server --
foundInfo: GVLocate.FoundServerInfo = GVLocate.FindRegServer[name, Accept];
WITH foundInfo SELECT FROM
notFound => rc ← [BadRName, notFound];
allDown => rc ← [AllDown,notFound];
found =>
BEGIN
TryUpdate[ str ! Failed => GOTO down ];
EXITS down =>
{ Destroy[]; rc ← [AllDown,notFound] };
END;
ENDCASE => ERROR;
END;
END;
WriteStamp: PROC[out: IO.STREAM, stamp: Timestamp] =
BEGIN
out.PutF["[%b#%b,%t]",
[cardinal[stamp.net]], [cardinal[stamp.host]], [time[BasicTime.FromPupTime[stamp.time]]] ];
END;
WriteType: PROC[out: IO.STREAM, type: RNameType] =
BEGIN
out.PutRope[SELECT type FROM
group => "group",
individual => "individual",
notFound => "not found",
dead => "dead",
ENDCASE => ERROR];
END;
WriteRC: PROC[out: IO.STREAM, rc: ReturnCode] =
BEGIN
out.PutRope[SELECT rc.code FROM
done => "ok",
noChange => "no change",
outOfDate => "out of date",
NotAllowed => "not allowed",
BadOperation => "bad operation",
BadProtocol => "bad protocol",
BadRName => "bad R-Name: ",
BadPassword => "bad password",
WrongServer => "wrong server",
AllDown => "all suitable R-Servers down",
ENDCASE => ERROR];
IF rc.code = BadRName THEN WriteType[out, rc.type];
END;
WriteComponent: PROC[out: IO.STREAM, type: RNameType, i: CARDINAL] =
BEGIN
out.PutF["component=%g. ", [rope[
SELECT type FROM
group => SELECT i FROM
0 => "prefix",
1 => "remark",
2 => "members",
3 => "memberStamps",
4 => "delMembers",
5 => "delMemberStamps",
6 => "owners",
7 => "ownerStamps",
8 => "delOwners",
9 => "delOwnerStamps",
10 => "friends",
11 => "friendStamps",
12 => "delFriends",
13 => "delFriendStamps",
ENDCASE => ERROR,
individual => SELECT i FROM
0 => "prefix",
1 => "password",
2 => "connect",
3 => "forward",
4 => "forwardStamps",
5 => "delForward",
6 => "delForwardStamps",
7 => "inboxes",
8 => "inboxStamps",
9 => "delInboxes",
10 => "delInboxStamps",
ENDCASE => ERROR,
dead => SELECT i FROM
0 => "prefix",
ENDCASE => ERROR,
ENDCASE => ERROR]] ];
END;
ComponentType: TYPE = {rnames, stamps, other};
GetComponentType: PROC [type: RNameType, i: CARDINAL] RETURNS [ComponentType] =
BEGIN
SELECT type FROM
group => SELECT i FROM
2, 4, 6, 8, 10, 12 => RETURN [rnames];
3, 5, 7, 9, 11, 13 => RETURN [stamps];
ENDCASE;
individual => SELECT i FROM
3, 5, 7, 9 => RETURN [rnames];
4, 6, 8, 10 => RETURN [stamps];
ENDCASE;
ENDCASE;
RETURN[other];
END;
Complaint: PROC[out: IO.STREAM, server: Rope.ROPE, name: RName, rc: ReturnCode] =
BEGIN
out.PutF["\n%g (%g) ", [rope[name]], [rope[server]] ];
WriteRC[out, rc];
END;
Differ: PROC[out: IO.STREAM, name: RName, difference: Rope.ROPE] =
BEGIN
out.PutF["\n%g: %g different. ", [rope[name]], [rope[difference]] ];
END;
ReceiveString: PROC [from: Handle, to: STRING] =
BEGIN
length: CARDINAL = ReceiveCount[from];
maxlength: CARDINAL = ReceiveCount[from];
ReceiveBytes[from, [base: LOOPHOLE[LONG[@to.text]], count: ((length+1)/2)*2]];
to.length ← length;
END;
CompareComponent: PROC[out: IO.STREAM, type: RNameType, count: CARDINAL, name: RName] =
BEGIN
differ: BOOLEANFALSE;
bLength: CARDINAL = 64;
buffer1: PACKED ARRAY [0..bLength) OF CHARACTER;
buffer2: PACKED ARRAY [0..bLength) OF CHARACTER;
string1: STRING = [64];
string2: STRING = [64];
length1: CARDINAL ← ReceiveCount[targetStr];
length2: CARDINAL ← ReceiveCount[str];
stamp1: Timestamp;
stamp2: Timestamp;
IF length1 # length2
THEN BEGIN
Differ[out, name, "component length"];
WriteComponent[out, type, count];
END;
SELECT GetComponentType[type, count] FROM
rnames => WHILE length1 > 0 OR length2 > 0 DO
string1.length ← 0;
string2.length ← 0;
IF length1 > 0 THEN BEGIN
ReceiveString[targetStr, string1];
length1 ← length1 - (string1.length + 1) / 2 - 2;
END;
IF length2 > 0 THEN BEGIN
ReceiveString[str, string2];
length2 ← length2 - (string2.length + 1) / 2 - 2;
END;
IF string1.length # string2.length
THEN differ ← TRUE
ELSE FOR i: CARDINAL IN [0..string1.length)
DO IF string1[i] # string2[i] THEN differ ← TRUE; ENDLOOP;
ENDLOOP;
stamps => WHILE length1 > 0 OR length2 > 0 DO
IF length1 > 0 THEN BEGIN
stamp1 ← ReceiveTimestamp[targetStr];
length1 ← length1 - SIZE[Timestamp];
END;
IF length2 > 0 THEN BEGIN
stamp2 ← ReceiveTimestamp[str];
length2 ← length2 - SIZE[Timestamp];
END;
IF stamp1 # stamp2 THEN differ ← TRUE;
ENDLOOP;
ENDCASE => WHILE length1 > 0 OR length2 > 0 DO
wanted1: CARDINAL = 2*MIN[bLength/2, length1] --bytes--;
wanted2: CARDINAL = 2*MIN[bLength/2, length2];
IF wanted1 > 0 THEN ReceiveBytes[targetStr, [base: LOOPHOLE[LONG[@buffer1]], count: wanted1]];
IF wanted2 > 0 THEN ReceiveBytes[str, [base: LOOPHOLE[LONG[@buffer2]], count: wanted2]];
length1 ← length1 - wanted1/2;
length2 ← length2 - wanted2/2;
IF count # 0 THEN FOR i: CARDINAL IN [0..MIN[wanted1,wanted2]) DO
IF buffer1[i] # buffer2[i] THEN differ ← TRUE;
ENDLOOP;
ENDLOOP;
IF differ AND count # 0
THEN {Differ[out, name, "component contents"];
WriteComponent[out, type, count]};
END;
SkipComponent: PROC[thisStr: Handle] =
BEGIN
bLength: CARDINAL = 64;
buffer1: PACKED ARRAY [0..bLength) OF CHARACTER;
length1: CARDINAL ← ReceiveCount[targetStr];
WHILE length1 > 0
DO wanted1: CARDINAL = 2*MIN[bLength/2, length1] --bytes--;
ReceiveBytes[thisStr, [base: LOOPHOLE[LONG[@buffer1]], count: wanted1]];
length1 ← length1 - wanted1/2;
ENDLOOP;
END;
Look: PROC[out: IO.STREAM, name: GVBasics.RName]RETURNS[done: BOOLEAN] =
BEGIN
ok: BOOLEANTRUE;
stamp1, stamp2: Timestamp;
rc1, rc2: ReturnCode;
[rc1,stamp1] ← Enquire[targetStr, ReadEntry, name, oldestTime];
rc2 ← ReadElsewhere[out: out, name: name];
IF rc2.code = done THEN stamp2 ← ReceiveTimestamp[str];
out.PutChar['!];
IF rc1.code # done
THEN { Complaint[out, "target server", name, rc1]; ok←FALSE };
IF rc2.code # done
THEN { Complaint[out, "other server", name, rc2]; ok←FALSE };
IF rc1.type # rc2.type
THEN { Differ[out, name, "types"]; ok←FALSE };
BEGIN
count1: CARDINALIF rc1.code # done THEN 0
ELSE ReceiveCount[targetStr];
count2: CARDINALIF rc2.code # done THEN 0
ELSE ReceiveCount[str];
IF ok AND count1 # count2 THEN ERROR;
IF ok AND stamp1 # stamp2 THEN Differ[out, name, "global stamps"];
IF ok THEN FOR c: CARDINAL IN [0..MIN[count1,count2])
DO count1 ← count1-1;
count2 ← count2-1;
CompareComponent[out, rc1.type, c, name];
ENDLOOP;
THROUGH [1..count1] DO SkipComponent[targetStr] ENDLOOP;
THROUGH [1..count2] DO SkipComponent[str] ENDLOOP;
END;
done ← (rc1.code=WrongServer OR rc2.code=WrongServer)
OR (rc1.code=AllDown OR rc2.code=AllDown);
END;
LookEnum: PROC[out: IO.STREAM, enumName: GVBasics.RName,
work: PROC[IO.STREAM, GVBasics.RName]RETURNS[done: BOOLEAN] ] =
argument is "groups.reg", "individuals.reg", or "dead.reg" --
BEGIN
out.PutChar['\n];
out.Put[[rope[enumName]]];
BEGIN
memberInfo: GVNames.MemberInfo = GVNames.GetMembers[enumName];
IF targetStr = NIL THEN targetStr ← CreateStream[targetAddr, RSEnquiry];
WITH memberInfo SELECT FROM
group => BEGIN
out.PutChar['\n];
FOR m: LIST OF GVBasics.RName ← members, m.rest UNTIL m = NIL
DO IF work[out, m.first] THEN EXIT ENDLOOP;
END;
allDown => out.PutRope[": all R-Servers down!"];
notFound => out.PutRope[": not found!"];
ENDCASE => ERROR;
IF targetStr # NIL THEN { targetStr.Close[]; targetStr←NIL };
END;
END;
LookAtRegistry: PROC[out: IO.STREAM, regGroup: GVBasics.RName]RETURNS[done: BOOLEAN] =
BEGIN
dotPos: INT = regGroup.Find["."];
reg: Rope.ROPE = regGroup.Substr[0,dotPos];
IF targetStr # NIL THEN { targetStr.Close[]; targetStr←NIL };
SELECT GVNames.IsMemberDirect[regGroup, targetRope] FROM
yes => NULL;
no => RETURN[FALSE]; -- target isn't in this registry --
notGroup => ERROR;
allDown => NULL;
ENDCASE => ERROR;
out.PutF["\n\n%t Registry %g\n", IO.time[], [rope[reg]]];
LookEnum[out, Rope.Cat["Groups.", reg], Look];
LookEnum[out, Rope.Cat["Individuals.", reg], Look];
LookEnum[out, Rope.Cat["Dead.", reg], Look];
done ← FALSE;
END;
LookAtAll: ENTRY PROC =
BEGIN
out: IO.STREAM = ViewerIO.CreateViewerStreams["GV database comparison"].out;
out.PutF["%t Comparing database on %g with rest of world.\n", IO.time[], [rope[targetRope]]];
LookEnum[out, "Groups.gv", LookAtRegistry ! GVProtocol.Failed =>
CHECKED{ out.PutRope["GVProtocol.Failed"]; CONTINUE }];
out.PutF["\n\n%t Finished!\n", IO.time[]];
END;
GetStream: PROC[out, in: IO.STREAM] RETURNS[ok: BOOLEAN] =
BEGIN
info: GVNames.ConnectInfo;
targetConnect: GVBasics.Connect;
targetRope ← in.GetTokenRope[].token;
IF targetRope.Find["."] >= 0 THEN [info, targetConnect] ← GVNames.GetConnect[targetRope]
ELSE { info ← individual; targetConnect ← targetRope };
SELECT info FROM
individual => NULL;
ENDCASE => { out.PutRope[" bad name"]; RETURN[FALSE] };
targetAddr ← PupName.NameLookup[targetConnect, Pup.nullSocket];
RETURN[TRUE];
END;
DoIt: ENTRY Commander.CommandProc = TRUSTED
BEGIN ENABLE UNWIND => NULL;
IF GetStream[cmd.out, IO.RIS[cmd.commandLine]]
THEN Process.Detach[FORK LookAtAll[]];
IF targetStr # NIL THEN targetStr.Close[];
IF str # NIL THEN str.Close[];
END;
Commander.Register["DBCompare", DoIt, "Compare GV database on given server"];
END.