LoganBerryCommandsImpl.mesa
Copyright © 1986 by Xerox Corporation. All rights reserved.
Doug Terry, January 28, 1987 9:09:29 pm PST
Registers a collection of CommandTool commands for performing operations on a LoganBerry database.
DIRECTORY
Commander USING [CommandProc, Handle, Register],
IO USING [atom, BreakProc, EndOfStream, GetTokenRope, IDProc, PutF, PutRope, TokenProc, RIS, rope, STREAM],
LoganBerryBrowser USING [AttributePatterns, CreateTool, FilteredQuery, PatternsToEntry, ReadAttributePatterns, SyntaxError, Tool, ToolBody],
LoganBerryStub USING [AttributeType, AttributeValue, BuildIndices, Close, CompactLogs, DeleteEntry, Describe, Entry, EntryProc, Error, Open, OpenDB, SchemaInfo, WriteEntry],
Process USING [CheckForAbort],
Rope USING [Equal, ROPE];
LoganBerryCommandsImpl: CEDAR PROGRAM
IMPORTS Commander, IO, LoganBerry: LoganBerryStub, LoganBerryBrowser, Process, Rope
~ BEGIN
ROPE: TYPE ~ Rope.ROPE;
STREAM: TYPE ~ IO.STREAM;
currentName: ROPE ← "#";
currentDB: LoganBerry.OpenDB; -- cache of most recently used db
LoganBerry commands
DescribeCmd: Commander.CommandProc = {
[cmd: Commander.Handle] RETURNS [result: REF ANY ← NIL, msg: ROPE ← NIL]
ENABLE LoganBerry.Error => {
IO.PutF[cmd.err, "Error: %g - %g\n", IO.atom[ec], IO.rope[explanation]];
GOTO End;
};
dbname: ROPE;
info: LoganBerry.SchemaInfo;
[dbname,,msg] ← ParseArgs[cmd];
IF msg#NIL THEN RETURN;
info ← LoganBerry.Describe[db: Open[dbname]];
IO.PutF[cmd.out, "Database: %g\n", IO.rope[info.dbName]];
IO.PutRope[cmd.out, " keys:"];
FOR k: LIST OF LoganBerry.AttributeType ← info.keys, k.rest WHILE k # NIL DO
IO.PutF[cmd.out, " %g", IO.atom[k.first]];
ENDLOOP;
IO.PutRope[cmd.out, "\n"];
IO.PutRope[cmd.out, " index files:"];
FOR l: LIST OF ROPE ← info.indexNames, l.rest WHILE l # NIL DO
IO.PutF[cmd.out, " %g", IO.rope[l.first]];
ENDLOOP;
IO.PutRope[cmd.out, "\n"];
IO.PutRope[cmd.out, " log files:"];
FOR l: LIST OF ROPE ← info.logNames, l.rest WHILE l # NIL DO
IO.PutF[cmd.out, " %g", IO.rope[l.first]];
ENDLOOP;
IO.PutRope[cmd.out, "\n"];
EXITS
End => RETURN;
};
QueryCmd: Commander.CommandProc = {
[cmd: Commander.Handle] RETURNS [result: REF ANY ← NIL, msg: ROPE ← NIL]
ENABLE LoganBerry.Error => {
IO.PutF[cmd.err, "Error: %g - %g\n", IO.atom[ec], IO.rope[explanation]];
GOTO End;
};
PrintEntry: LoganBerry.EntryProc = {
[entry: LoganBerry.Entry] RETURNS [continue: BOOL]
Process.CheckForAbort[];
FOR e: LoganBerry.Entry ← entry, e.rest UNTIL e = NIL DO
IO.PutF[cmd.out, "%g: %g\n", IO.atom[e.first.type], IO.rope[e.first.value]];
ENDLOOP;
IO.PutRope[cmd.out, "\n"];
RETURN[TRUE];
};
dbname: ROPE;
ap: LoganBerryBrowser.AttributePatterns;
db: LoganBerry.OpenDB;
[dbname, ap, msg] ← ParseArgs[cmd];
IF msg#NIL THEN RETURN;
db ← Open[dbname];
LoganBerryBrowser.FilteredQuery[db: db, patterns: ap, proc: PrintEntry];
EXITS
End => RETURN;
};
WriteCmd: Commander.CommandProc = {
[cmd: Commander.Handle] RETURNS [result: REF ANY ← NIL, msg: ROPE ← NIL]
ENABLE LoganBerry.Error => {
IO.PutF[cmd.err, "Error: %g - %g\n", IO.atom[ec], IO.rope[explanation]];
GOTO End;
};
dbname: ROPE;
ap: LoganBerryBrowser.AttributePatterns;
db: LoganBerry.OpenDB;
entry: LoganBerry.Entry;
[dbname, ap, msg] ← ParseArgs[cmd];
IF msg#NIL THEN RETURN;
db ← Open[dbname];
entry ← LoganBerryBrowser.PatternsToEntry[ap];
LoganBerry.WriteEntry[db: db, entry: entry];
EXITS
End => RETURN;
};
DeleteCmd: Commander.CommandProc = {
[cmd: Commander.Handle] RETURNS [result: REF ANY ← NIL, msg: ROPE ← NIL]
ENABLE LoganBerry.Error => {
IO.PutF[cmd.err, "Error: %g - %g\n", IO.atom[ec], IO.rope[explanation]];
GOTO End;
};
GetAttributeValue: PROC [entry: LoganBerry.Entry, type: LoganBerry.AttributeType] RETURNS [LoganBerry.AttributeValue] ~ {
FOR e: LoganBerry.Entry ← entry, e.rest WHILE e # NIL DO
IF e.first.type = type THEN
RETURN[e.first.value];
ENDLOOP;
RETURN[NIL];
};
DeleteEntry: LoganBerry.EntryProc = {
[entry: LoganBerry.Entry] RETURNS [continue: BOOL]
value: LoganBerry.AttributeValue ← GetAttributeValue[entry, primaryKey];
Process.CheckForAbort[];
LoganBerry.DeleteEntry[db: db, key: primaryKey, value: value];
IO.PutF[cmd.out, "Deleted %g: %g\n", IO.atom[primaryKey], IO.rope[value]];
RETURN[TRUE];
};
dbname: ROPE;
ap: LoganBerryBrowser.AttributePatterns;
db: LoganBerry.OpenDB;
primaryKey: LoganBerry.AttributeType;
[dbname, ap, msg] ← ParseArgs[cmd];
IF msg#NIL THEN RETURN;
db ← Open[dbname];
primaryKey ← LoganBerry.Describe[db: db].info.keys.first;
LoganBerryBrowser.FilteredQuery[db: db, patterns: ap, proc: DeleteEntry];
EXITS
End => RETURN;
};
CloseCmd: Commander.CommandProc = {
[cmd: Commander.Handle] RETURNS [result: REF ANY ← NIL, msg: ROPE ← NIL]
ENABLE LoganBerry.Error => {
IO.PutF[cmd.err, "Error: %g - %g\n", IO.atom[ec], IO.rope[explanation]];
GOTO End;
};
dbname: ROPE;
[dbname,,msg] ← ParseArgs[cmd];
IF msg#NIL THEN RETURN;
LoganBerry.Close[db: Open[dbname]];
EXITS
End => RETURN;
};
BuildIndicesCmd: Commander.CommandProc = {
[cmd: Commander.Handle] RETURNS [result: REF ANY ← NIL, msg: ROPE ← NIL]
ENABLE LoganBerry.Error => {
IO.PutF[cmd.err, "Error: %g - %g\n", IO.atom[ec], IO.rope[explanation]];
GOTO End;
};
dbname: ROPE;
[dbname,,msg] ← ParseArgs[cmd];
IF msg#NIL THEN RETURN;
LoganBerry.BuildIndices[db: Open[dbname]];
EXITS
End => RETURN;
};
CompactLogsCmd: Commander.CommandProc = {
[cmd: Commander.Handle] RETURNS [result: REF ANY ← NIL, msg: ROPE ← NIL]
ENABLE LoganBerry.Error => {
IO.PutF[cmd.err, "Error: %g - %g\n", IO.atom[ec], IO.rope[explanation]];
GOTO End;
};
dbname: ROPE;
[dbname,,msg] ← ParseArgs[cmd];
IF msg#NIL THEN RETURN;
LoganBerry.CompactLogs[db: Open[dbname]];
EXITS
End => RETURN;
};
MakeBrowserTool: Commander.CommandProc = {
[cmd: Commander.Handle] RETURNS [result: REF ANY ← NIL, msg: ROPE ← NIL]
ENABLE LoganBerry.Error => {
IO.PutF[cmd.err, "Error: %g - %g\n", IO.atom[ec], IO.rope[explanation]];
GOTO End;
};
dbname: ROPE;
tool: LoganBerryBrowser.Tool ← NEW[LoganBerryBrowser.ToolBody];
argStream: IO.STREAMIO.RIS[cmd.commandLine];
dbname ← IO.GetTokenRope[argStream, IO.IDProc ! IO.EndOfStream => {msg ← "No database name supplied.\n"; GOTO End}].token;
LoganBerryBrowser.CreateTool[db: Open[dbname], tool: tool];
EXITS
End => RETURN;
};
ParseArgs: PROC [cmd: Commander.Handle] RETURNS [dbname: ROPENIL, ap: LoganBerryBrowser.AttributePatterns ← NIL, errMsg: ROPENIL] ~ {
argStream: IO.STREAMIO.RIS[cmd.commandLine];
dbname ← IO.GetTokenRope[argStream, IO.IDProc ! IO.EndOfStream => {errMsg ← "No database name supplied.\n"; GOTO End}].token;
ap ← LoganBerryBrowser.ReadAttributePatterns[argStream ! LoganBerryBrowser.SyntaxError => {errMsg ← explanation; GOTO End}];
EXITS
End => RETURN;
};
Open: PROC [dbname: ROPE] RETURNS [db: LoganBerry.OpenDB] ~ {
Opens the database with the given name; a distinquished name "#" indicates that the most recently opened database should be used. A cache of opened databases could be maintained, but we don't bother for now.
IF Rope.Equal[dbname, currentName] THEN RETURN[currentDB];
db ← currentDB ← LoganBerry.Open[dbName: dbname];
};
Command registrations
Commander.Register[key: "LBDescribe", proc: DescribeCmd, interpreted: FALSE,
doc: "Get schema information about a LoganBerry database.\n usage: LBDescribe <dbname>" ];
Commander.Register[key: "LBQuery", proc: QueryCmd, interpreted: FALSE,
doc: "Query a LoganBerry database.\n usage: LBQuery <dbname> <atype(pattern):avalue> ..." ];
Commander.Register[key: "LBRead", proc: QueryCmd, interpreted: FALSE,
doc: "synonym for LBQuery." ];
Commander.Register[key: "LBWrite", proc: WriteCmd, interpreted: FALSE,
doc: "Write an entry into a LoganBerry database.\n usage: LBWrite <dbname> <atype:avalue> ..." ];
Commander.Register[key: "LBDelete", proc: DeleteCmd, interpreted: FALSE,
doc: "Delete entries from a LoganBerry database.\n usage: LBDelete <dbname> <atype(pattern):avalue> ..." ];
Commander.Register[key: "LBClose", proc: CloseCmd, interpreted: FALSE,
doc: "Close a LoganBerry database.\n usage: LBClose <dbname>" ];
Commander.Register[key: "LBBuildIndices", proc: BuildIndicesCmd, interpreted: FALSE,
doc: "Rebuild the indices for a LoganBerry database.\n usage: LBBuildIndices <dbname>" ];
Commander.Register[key: "LBCompactLogs", proc: CompactLogsCmd, interpreted: FALSE,
doc: "Compact a LoganBerry database.\n usage: LBCompactLogs <dbname>" ];
Commander.Register[key: "LoganBerryBrowser", proc: MakeBrowserTool,
doc: "Create a LoganBerry browser." ];
Commander.Register[key: "LBBrowser", proc: MakeBrowserTool,
doc: "Create a LoganBerry browser." ];
END.
Doug Terry, October 20, 1986 10:45:54 am PDT
Created.
changes to: DIRECTORY, LoganBerryCommandsImpl, IMPORTS, EXPORTS, DescribeCmd, QueryCmd, WriteCmd, DeleteCmd, CloseCmd, BuildIndicesCmd, CompactLogsCmd, ParseArgs, Commander, Commander, Commander, Commander, Commander, Commander, Commander, Commander, END, ~, ParseArgs, AttributeBreakProc (local of ReadAttributePatterns), ReadAttributePattern (local of ReadAttributePatterns), Open, WriteAttributePatterns, DoQuery, NamedFilter, WriteAttributePattern (local of WriteAttributePatterns), OptimalStart, PrintEntry (local of QueryCmd), DeleteEntry (local of DeleteCmd), GetAttributeValue, FilteredQuery
Doug Terry, October 31, 1986 2:58:29 pm PST
Exports LoganBerryBrowser.
changes to: DIRECTORY, EXPORTS, QueryCmd, WriteCmd, DeleteCmd, ParseArgs, ReadAttributePatterns, ReadAttributePattern (local of ReadAttributePatterns), WriteAttributePatterns, WriteAttributePattern (local of WriteAttributePatterns), PatternsToEntry, EntryToPatterns, FilteredQuery
Doug Terry, November 2, 1986 6:38:02 pm PST
Added support for subrange filter.
changes to: DIRECTORY, OptimalStart, NamedFilter, ToDash, Subrange, GetAttributeValue
Doug Terry, November 23, 1986 8:59:16 pm PST
Moved support for filtered queries to LoganBerryBrowserImpl; added command for creating a browser. Now this is simply a client of LoganBerry and LoganBerryBrowser.
changes to: ~, ParseArgs, Open, Commander, MakeBrowserTool
Doug Terry, January 28, 1987 9:09:29 pm PST
changes to: MakeBrowserTool, Open, CheckOps