ScriptDBImpl.mesa
Copyright Ó 1987 by Xerox Corporation. All rights reserved.
Polle Zellweger (PTZ) January 28, 1987 6:58:29 pm PST
Routines for storing scripts in a database and manipulating their structure.
DIRECTORY
BasicTime USING [GMT, earliestGMT, Now, Period, Update],
Convert USING [AtomFromRope, BoolFromRope, IntFromRope, TimeFromRope, RopeFromAtom, RopeFromBool, RopeFromInt, RopeFromTime],
FS USING [ComponentPositions, ExpandName],
IO USING [card, GetCard, GetInt, int, PutFR, RIS, rope, STREAM],
LoganBerry USING [AttributeType, AttributeValue, Cursor, DeleteEntry, EndGenerate, Entry, Error, GenerateEntries, NextEntry, Open, OpenDB, ReadEntry, WriteEntry],
Rope USING [Concat, Equal, Find, Replace, ROPE, Substr],
Thrush USING [EncryptionKey, Tune, VoiceInterval],
UserCredentials USING [CredentialsChangeProc, Get, RegisterForChange],
ScriptDB;
ScriptDBImpl: CEDAR PROGRAM -- Should this be a monitor?
IMPORTS BasicTime, Convert, FS, IO, Rope, UserCredentials, LoganBerry: LoganBerryStub
EXPORTS ScriptDB
~ BEGIN
OPEN ScriptDB;
ROPE: TYPE ~ Rope.ROPE;
STREAM: TYPE ~ IO.STREAM;
Script database operations
Script descriptors are stored in a LoganBerry database. Script entry descriptors are also maintained in a LoganBerry database.
Error: PUBLIC ERROR [ec: ATOM, explanation: Rope.ROPENIL] = CODE;
Open: PUBLIC PROC [dbName: Rope.ROPE] RETURNS [handle: Handle ← NIL] = {
Prepares the given database for service. The name of the entries database is derived from the script database name by adding "Entries" after the base name.
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
sDB, eDB: ROPE;
cp: FS.ComponentPositions;
IF dbName = NIL THEN RETURN[NIL];
[sDB, cp] ← FS.ExpandName[dbName];
IF cp.ext.length = 0 THEN
sDB ← Rope.Concat[sDB, ".df"];
eDB ← Rope.Replace[base: sDB, start: cp.base.start+cp.base.length, len: 0, with: "Entries"];
handle ← NEW[HandleRec ← [scriptDBName: sDB, scriptEntriesDBName: eDB]];
handle.scriptDB ← LoganBerry.Open[dbName: handle.scriptDBName];
handle.scriptEntriesDB ← LoganBerry.Open[dbName: handle.scriptEntriesDBName];
};
ReadScriptDesc: PUBLIC PROC [handle: Handle, sid: ScriptID] RETURNS [script: Script] ~ {
Returns the descriptor of the given script.
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
lbe: LBEntry ← NIL;
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
lbe ← LoganBerry.ReadEntry[db: handle.scriptDB, key: $SID, value: sid].entry;
script ← LBEntryToScriptDesc[lbe];
};
WriteScriptDesc: PUBLIC PROC [handle: Handle, script: Script] ~ {
Writes the script descriptor into the LoganBerry database.
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
lbe: LoganBerry.Entry ← ScriptDescToLBEntry[script];
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
LoganBerry.WriteEntry[db: handle.scriptDB, entry: lbe];
 ! LoganBerry.Error => IF ec = $ValueNotUnique THEN {script.sid ← ReplaceID[lbe]; RETRY} -- currently, script IDs are created long before they are presented to the db for storage. Others probably have already included references to them. Therefore, if they are not already unique, we got trouble.
};
DeleteScriptDesc: PUBLIC PROC [handle: Handle, sid: ScriptID] ~ {
Deletes the given script (regardless of what entries may exist).
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
LoganBerry.DeleteEntry[db: handle.scriptDB, key: $SID, value: sid];
};
FindScriptsStartingInFile: PUBLIC PROC [handle: Handle, file: FileID] RETURNS [scriptList: ScriptList]~ {
Find all scripts that start in the given file. This includes all scripts with an internal first entry whose create date is not later than the file and all scripts with an external first entry whose create date is equal to the file.
For this to work as written, filenames should not have version parts!
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
continue: BOOLEANTRUE;
cursor: LoganBerry.Cursor;
lbe: LoganBerry.Entry;
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
scriptList ← NIL;
cursor ← LoganBerry.GenerateEntries[db: handle.scriptDB, key: $StartingFile, start: file.name];
WHILE (lbe ← LoganBerry.NextEntry[cursor: cursor]) # NIL DO
script ← LBEntryToScriptDesc[lbe];
fEntry ← ReadScriptEntryDesc[handle, script.firstEntry];
IF (fEntry.internal AND script.startingFile.createTime>=file.createTime)
OR script.startingFile.createTime=file.createTime THEN
scriptList ← CONS[script, scriptList];
ENDLOOP;
LoganBerry.EndGenerate[cursor: cursor];
};
ReadScriptEntryDesc: PUBLIC PROC [handle: Handle, eid: ScriptEntryID] RETURNS [scriptEntry: ScriptEntry] ~ {
Returns the descriptor of the given script.
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
lbe: LBEntry ← NIL;
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
lbe ← LoganBerry.ReadEntry[db: handle.scriptEntryDB, key: $EID, value: eid].entry;
scriptEntry ← LBEntryToScriptEntryDesc[lbe];
};
WriteScriptEntryDesc: PUBLIC PROC [handle: Handle, scriptEntry: ScriptEntry] ~ {
Writes the script descriptor into the LoganBerry database.
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
lbe: LoganBerry.Entry ← ScriptEntryDescToLBEntry[scriptEntry];
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
LoganBerry.WriteEntry[db: handle.scriptEntryDB, entry: lbe];
 ! LoganBerry.Error => IF ec = $ValueNotUnique THEN {scriptEntry.eid ← ReplaceID[lbe]; RETRY} -- currently, script entry IDs are created long before they are presented to the db for storage. Others probably have already included references to them. Therefore, if they are not already unique, we got trouble.
};
DeleteScriptEntryDesc: PUBLIC PROC [handle: Handle, eid: ScriptEntryID] ~ {
Deletes the given script entry (regardless of what entries may exist).
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
LoganBerry.DeleteEntry[db: handle.scriptEntryDB, key: $EID, value: eid];
};
Voice rope structure
WARNING: For efficiency reasons, much of this code relies on the exact structure of entries stored in a LoganBerry database. For instance, it assumes that the first attribute of an entry is a voice rope (or interest) ID and does not check the attribute type to verify this. Do NOT reorder attributes unless care is taken to change the code dependencies.
A tune list, the structure of a voice rope, is represented in a LoganBerry database as an ordered list of $TID (the tune's ID), $Key (the tune's encryption key), and $SL (the start and length of a tune interval) attributes. The tune's encryption key should probably be kept in the tune's header, but that can't be done if FinchSmarts is used to record and play tunes.
ScriptDescToLBEntry: PUBLIC PROC [s: Script] RETURNS [lbe: LBEntry] ~ {
Converts a script descriptor to a list of (attribute, value) pairs suitable for storing in the db.
lbe ← LIST[NEW[Attribute ← [$StartingFiledate, Convert.RopeFromTime[from: s.startingFile.createTime, end: seconds]]]];
lbe ← CONS[NEW[Attribute ← [$StartingFilename, s.startingFile.name], list]];
lbe ← CONS[NEW[Attribute ← [$NumEntries, Convert.RopeFromInt[s.numEntries]], list]];
lbe ← CONS[NEW[Attribute ← [$Creation, Convert.RopeFromTime[from: s.sCreateInfo.creation, end: seconds]], list]];
lbe ← CONS[NEW[Attribute ← [$Creator, s.sCreateInfo.creator], list]];
lbe ← CONS[NEW[Attribute ← [$Desc, s.sDesc], list]];
lbe ← CONS[NEW[Attribute ← [$Name, s.sName], list]];
lbe ← CONS[NEW[Attribute ← [$FirstEntry, s.firstEntry.eid], list]];
lbe ← CONS[NEW[Attribute ← [$SID, s.sid], list]];
};
LBEntryToScriptDesc: PUBLIC PROC [lbe: LBEntry] RETURNS [s: Script] ~ {
Converts a list of (attribute, value) pairs from the db into a script descriptor.
s ← NEW[defaultScript];
WHILE lbe#NIL DO
SELECT lbe.first.type FROM
$SID => s.sid ← lbe.first.value;
$FirstEntry => s.firstEntry.eid ← lbe.first.value;
$Name => s.sName ← lbe.first.value;
$Desc => s.sDesc ← lbe.first.value;
$Creator => s.sCreateInfo.creator ← lbe.first.value;
$Creation => s.sCreateInfo.creation ← Convert.TimeFromRope[lbe.first.value];
$NumEntries => s.numEntries ← Convert.IntFromRope[lbe.first.value];
$StartingFilename => s.startingFile.name;
$StartingFiledate => s.startingFile.createTime ← Convert.TimeFromRope[lbe.first.value];
ENDCASE;
lbe ← lbe.rest;
ENDLOOP;
};
ScriptEntryDescToLBEntry: PUBLIC PROC [se: ScriptEntry] RETURNS [lbe: LBEntry] ~ {
Converts a script entry descriptor to a list of (attribute, value) pairs suitable for storing in the db.
lbe ← LIST[NEW[Attribute ← [$Creation, Convert.RopeFromTime[from: se.sCreateInfo.creation, end: seconds]], list]];
lbe ← CONS[NEW[Attribute ← [$Creator, se.sCreateInfo.creator], list]];
lbe ← CONS[NEW[Attribute ← [$Desc, se.eDesc], list]];
lbe ← CONS[NEW[Attribute ← [$Name, se.eName], list]];
lbe ← CONS[NEW[Attribute ← [$NextEntries, FlattenNextEntries[se.nextEntries]], list]];
lbe ← CONS[NEW[Attribute ← [$PauseAfter, Convert.RopeFromInt[se.pauseAfter]], list]];
lbe ← CONS[NEW[Attribute ← [$PauseBefore, Convert.RopeFromInt[se.pauseBefore]], list]];
lbe ← CONS[NEW[Attribute ← [$Action, se.action], list]];
lbe ← CONS[NEW[Attribute ← [$CharsLen, Convert.RopeFromInt[se.chars.len]], list]];
lbe ← CONS[NEW[Attribute ← [$CharsStart, Convert.RopeFromInt[se.chars.start]], list]];
lbe ← CONS[NEW[Attribute ← [$Class, Convert.RopeFromAtom[se.class]], list]];
lbe ← CONS[NEW[Attribute ← [$Internal, Convert.RopeFromBool[se.internal]], list]];
lbe ← CONS[NEW[Attribute ← [$Filedate, Convert.RopeFromTime[from: se.file.createTime, end: seconds]]]];
lbe ← CONS[NEW[Attribute ← [$Filename, se.file.name], list]];
lbe ← CONS[NEW[Attribute ← [$SID, se.scriptHeader.sid], list]];
lbe ← CONS[NEW[Attribute ← [$EID, se.eid], list]];
};
LBEntryToScriptEntryDesc: PUBLIC PROC [lbe: LBEntry] RETURNS [s:e ScriptEntry] ~ {
Converts a list of (attribute, value) pairs from the db into a script entry descriptor.
se ← NEW[defaultScriptEntry];
WHILE lbe#NIL DO
SELECT lbe.first.type FROM
$EID => se.eid ← lbe.first.value;
$SID => se.scriptHeader.sid ← lbe.first.value;
$Filename => se.file.name ← lbe.first.value;
$Filedate => se.file.createTime ← Convert.TimeFromRope[lbe.first.value];
$Internal => se.internal ← Convert.BoolFromRope[lbe.first.value];
$Class => se.class ← Convert.AtomFromRope[lbe.first.value];
$CharsStart => se.chars.start ← Convert.IntFromRope[lbe.first.value];
$CharsLen => se.chars.len ← Convert.IntFromRope[lbe.first.value];
$Action => se.action ← lbe.first.value;
$PauseBefore => se.pauseBefore ← Convert.IntFromRope[lbe.first.value];
$PauseAfter => se.pauseAfter ← Convert.IntFromRope[lbe.first.value];
$NextEntries => se.nextEntries ← ParseNextEntries[lbe.first.value];
$Name => se.eName ← lbe.first.value;
$Desc => se.eDesc ← lbe.first.value;
$Creator => se.sCreateInfo.creator ← lbe.first.value;
$Creation => se.sCreateInfo.creation ← Convert.TimeFromRope[lbe.first.value];
ENDCASE;
lbe ← lbe.rest;
ENDLOOP;
};
FlattenNextEntries: PROC [nextEntryList: NextEntryList] RETURNS [r: ROPE] ~ {
WHILE nextEntryList#NIL DO
r ← r.Concat[nextEntryList.first.cond, nextEntryList.first.entry.???]
nextEntryList ← nextEntryList.rest
ENDLOOP;
};
ParseNextEntries: PROC [r: ROPE] RETURNS [nextEntryList: NextEntryList] ~ {
nextEntryList ← NEW[];
};
EntryToTuneList: PROC [entry: LoganBerry.Entry] RETURNS [struct: TuneList] ~ {
Returns the tune list representing the structure of the voice rope.
IF entry = NIL THEN RETURN[NIL];
UNTIL entry.first.type = $TID DO
entry ← entry.rest;
IF entry = NIL THEN Error[$BadTuneList, "No TID in entry"];
ENDLOOP;
struct ← entry;
};
TuneListInterval: PUBLIC PROC [list: TuneList, interval: Thrush.VoiceInterval] RETURNS [new: TuneList] ~ {
Returns the tune list representing the structure of the voice rope interval. Warning: this operation modifies the original list!
tuneInterval: Thrush.VoiceInterval; -- the current tune's interval
prevSamples: INT; -- number of samples in entry excluding the current tune interval
samples: INT; -- number of samples in entry including the current tune interval
intervalMustChange: BOOLEANFALSE;
IF list = NIL THEN RETURN[NIL];
IF interval.length = -1 THEN interval.length ← LAST[INT];
prevSamples ← 0;
tuneInterval ← UnmarshalInterval[list.rest.rest.first.value];
samples ← tuneInterval.length;
UNTIL samples > interval.start DO
prevSamples ← samples;
list ← list.rest.rest.rest;
IF list = NIL THEN RETURN[NIL];
tuneInterval ← UnmarshalInterval[list.rest.rest.first.value];
samples ← samples + tuneInterval.length;
ENDLOOP;
at this point: prevSamples <= interval.start < samples
new ← list;
IF prevSamples # interval.start THEN { -- don't want first part of existing interval
tuneInterval.start ← tuneInterval.start + interval.start - prevSamples;
tuneInterval.length ← tuneInterval.length - (interval.start - prevSamples);
intervalMustChange ← TRUE;
};
Note: there's a possibility that interval.start + interval.length could cause an integer overflow; do I want to pay the cost to check for this?
IF (interval.length # LAST[INT]) AND (samples >= interval.start + interval.length) THEN { -- interval contained within a single tune interval
tuneInterval.length ← interval.length;
list.rest.rest.rest ← NIL; -- truncate list since we have has much as we need
intervalMustChange ← TRUE;
};
IF intervalMustChange THEN
new.rest.rest.first.value ← MarshalInterval[tuneInterval];
IF (interval.length = LAST[INT]) OR (samples >= interval.start + interval.length) THEN -- no need to go on
RETURN[new];
UNTIL samples >= interval.start + interval.length DO
prevSamples ← samples;
list ← list.rest.rest.rest;
IF list = NIL THEN RETURN[new];
tuneInterval ← UnmarshalInterval[list.rest.rest.first.value];
samples ← samples + tuneInterval.length;
ENDLOOP;
at this point: prevSamples <= interval.start + interval.length <= samples
IF samples # interval.start + interval.length THEN { -- want only first part of tune interval
list.rest.rest.first.value ← MarshalInterval[[start: tuneInterval.start, length: interval.start + interval.length - prevSamples]];
};
list.rest.rest.rest ← NIL; -- truncate list since we have has much as we need
RETURN[new];
};
PackHeader: PROC [info: VoiceRopeInfo] RETURNS [header: Header] ~ {
entry: LoganBerry.Entry;
entry ← info.struct;
entry ← CONS[[$Length, Convert.RopeFromInt[info.length]], entry];
the creator field is not really needed, but is included for compatibility with existing databases
entry ← CONS[[$Creator, info.creator], entry];
entry ← CONS[[$VRID, info.vrID], entry];
RETURN[entry];
};
ReplaceID: PROC [header: Header] RETURNS [newID: ID] ~ {
newID ← BadUniqueID[];
header.first.value ← newID;
};
UnpackHeader: PUBLIC PROC [header: Header] RETURNS [info: VoiceRopeInfo] ~ {
Returns the information associated with a voice rope header.
list: LoganBerry.Entry ← header;
IF header = NIL THEN RETURN;
info.vrID ← list.first.value;
list ← list.rest;
ignore creator field
list ← list.rest;
info.length ← Convert.IntFromRope[list.first.value];
[info.creator, info.timestamp] ← ParseUniqueID[info.vrID];
info.struct ← list.rest;
};
Interests database
AddInterest: PUBLIC PROC [handle: Handle, interest: InterestInfo] ~ {
Adds an entry to the interest database if a similar entry does not already exist.
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
entry: LoganBerry.Entry ← NIL;
oldEntry: LoganBerry.Entry;
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
oldEntry ← ReadInterest[handle, interest.vrID, interest.class, interest.refID];
interest.interestID ← IF oldEntry = NIL THEN GenerateUniqueID[] ELSE oldEntry.first.value;
entry ← PackInterest[interest];
LoganBerry.WriteEntry[db: handle.voiceInterestDB, entry: entry, replace: oldEntry # NIL ! LoganBerry.Error => IF ec = $ValueNotUnique AND oldEntry = NIL THEN {interest.interestID ← ReplaceInterestID[entry]; RETRY}];
};
DropInterest: PUBLIC PROC [handle: Handle, interest: InterestInfo] ~ {
Removes an entry from the interest database.
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
entry: LoganBerry.Entry;
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
entry ← ReadInterest[handle, interest.vrID, interest.class, interest.refID];
IF entry = NIL THEN RETURN;
LoganBerry.DeleteEntry[db: handle.voiceInterestDB, key: $IID, value: entry.first.value];
};
ReadInterest: PROC [handle: Handle, ropeID: ID ← NIL, class: Rope.ROPE ← NIL, refID: Rope.ROPE] RETURNS [entry: LoganBerry.Entry] ~ {
Searches for an entry with matching ropeID, class, and refID.
info: InterestInfo;
cursor: LoganBerry.Cursor ← LoganBerry.GenerateEntries[db: handle.voiceInterestDB, key: $RefID, start: refID, end: refID];
entry ← LoganBerry.NextEntry[cursor: cursor];
UNTIL entry = NIL DO
info ← UnpackInterest[entry];
IF (ropeID = NIL OR Rope.Equal[info.refID, refID]) AND (class = NIL OR Rope.Equal[info.class, class]) THEN EXIT;
entry ← LoganBerry.NextEntry[cursor: cursor];
ENDLOOP;
LoganBerry.EndGenerate[cursor: cursor];
};
InterestForRef: PUBLIC PROC [handle: Handle, class: Rope.ROPE, refID: Rope.ROPE] RETURNS [interest: Interest] ~ {
Returns an interest with the given class and refID, if one exists.
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
interest ← ReadInterest[handle: handle, class: class, refID: refID];
};
InterestInVoiceRope: PUBLIC PROC [handle: Handle, ropeID: ID] RETURNS [interest: Interest] ~ {
Returns an interest for the voice rope, if one exists.
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
interest ← LoganBerry.ReadEntry[db: handle.voiceInterestDB, key: $VRID, value: ropeID].entry;
};
EnumerateInterestClass: PUBLIC PROC [handle: Handle, class: Rope.ROPE, proc: InterestProc] ~ {
Calls the enumeration procedure for every interest of the given class. Stops when proc returns FALSE or the end of the database is encountered.
ENABLE LoganBerry.Error => ERROR Error[ec, explanation];
continue: BOOLEANTRUE;
entry: LoganBerry.Entry;
info: InterestInfo;
cursor: LoganBerry.Cursor;
IF handle = NIL THEN Error[$BadHandle, "NIL handle"];
IF proc = NIL THEN Error[$NoProc, "No procedure passed to EnumerateInterestClass"];
cursor ← LoganBerry.GenerateEntries[db: handle.voiceInterestDB, key: $Class, start: class, end: class];
entry ← LoganBerry.NextEntry[cursor: cursor];
UNTIL entry = NIL OR NOT continue DO
info ← UnpackInterest[entry];
continue ← proc[info];
entry ← LoganBerry.NextEntry[cursor: cursor];
ENDLOOP;
LoganBerry.EndGenerate[cursor: cursor];
};
PackInterest: PUBLIC PROC [info: InterestInfo] RETURNS [interest: Interest] ~ {
interest ← NIL;
IF info.data # NIL THEN
interest ← CONS[[$Data, info.data], interest];
interest ← CONS[[$RefID, info.refID], interest];
interest ← CONS[[$Class, info.class], interest];
interest ← CONS[[$VRID, info.vrID], interest];
interest ← CONS[[$IID, info.interestID], interest];
};
ReplaceInterestID: PROC [interest: Interest] RETURNS [newID: ID] ~ {
newID ← BadUniqueID[];
interest.first.value ← newID;
};
UnpackInterest: PUBLIC PROC [interest: Interest] RETURNS [info: InterestInfo] ~ {
list: LoganBerry.Entry ← interest;
IF interest = NIL THEN RETURN;
info.interestID ← list.first.value;
list ← list.rest;
info.vrID ← list.first.value;
list ← list.rest;
info.class ← list.first.value;
list ← list.rest;
info.refID ← list.first.value;
list ← list.rest;
IF list # NIL THEN
info.data ← list.first.value;
[info.creator, info.timestamp] ← ParseUniqueID[info.interestID];
};
Conversions (marshalling)
MarshalKey: PROC [key: Thrush.EncryptionKey] RETURNS [r: ROPE] ~ TRUSTED {
cardKey: LONG POINTER TO ARRAY[0..2) OF LONG CARDINAL=LOOPHOLE [LONG[@key]];
r ← IO.PutFR["%bB %bB", IO.card[cardKey[0]], IO.card[cardKey[1]]];
};
UnmarshalKey: PROC [r: ROPE] RETURNS [key: Thrush.EncryptionKey] ~ TRUSTED {
cardKey: LONG POINTER TO ARRAY[0..2) OF LONG CARDINAL=LOOPHOLE[LONG[@key]];
keyStream: IO.STREAMIO.RIS[r];
cardKey[0] ← IO.GetCard[keyStream];
cardKey[1] ← IO.GetCard[keyStream];
};
MarshalInterval: PROC [interval: Thrush.VoiceInterval] RETURNS [r: ROPE] ~ INLINE {
Writes a start and length field into a rope.
r ← IO.PutFR["%g %g", IO.int[interval.start], IO.int[interval.length]];
};
UnmarshalInterval: PROC [r: ROPE] RETURNS [interval: Thrush.VoiceInterval] ~ INLINE {
Parses the input rope into a start and length field.
s: IO.STREAM ← IO.RIS[r];
interval.start ← IO.GetInt[s];
interval.length ← IO.GetInt[s];
};
Generating unique identifiers (& keeping track of the username)
A unique identifier is generated by concatenating a processor id with a pseudo-timestamp. This scheme tries harder to ensure unique ids in the first place than the voice rope scheme (which uses Rname and pseudo-timestamp) because scriptids and scriptentryids are generated and probably stored long before they are saved in the databases.
Another alternative would be to store script headers and entries in the database right away, but that both makes it difficult to experiment with a script and increases the database storage and accesses. To allow experimentation, one could use id#temp, but then you're back to not knowing about uniqueness for id alone.
The pseudo-timestamp is taken to be the maximum of the current time (converted to an integer) and a simple counter. The current time alone is not sufficient since it has a granularity of seconds. Several script entries could conceivably be created within a second, but the long-term creation rate should be much less than one per second. The counter is initialized to the current time, which could cause some problems if this module is rerun before the current time has a chance to catch up to the old counter (or if a machine's clock is reset to an earlier time). This problem is solved by checking the counter's value with the databases in the initialization code.
This code depends on running on the client's machine.
userName: ROPE;
processorID: ROPE;
counter: INT;
GenerateUniqueID: PROC [] RETURNS [id: ROPE] ~ {
timestamp: INTMAX[counter, BasicTime.Period[BasicTime.earliestGMT, BasicTime.Now[]]];
id ← IO.PutFR["%g#%g", IO.rope[processorID], IO.int[timestamp]];
counter ← counter + 1;
};
ParseUniqueID: PROC [id: ROPE] RETURNS [creatingProcessor: ROPE, timestamp: BasicTime.GMT] ~ {
seconds: INT;
i: INT ← Rope.Find[s1: id, s2: "#"];
creatingProcessor ← Rope.Substr[base: id, start: 0, len: i];
seconds ← Convert.IntFromRope[Rope.Substr[base: id, start: i+1]];
timestamp ← BasicTime.Update[BasicTime.earliestGMT, seconds];
};
NewUser: UserCredentials.CredentialsChangeProc ~ {
userName ← UserCredentials.Get[].name;
};
InitUniqueID: PROC [] RETURNS [] ~ {
counter ← BasicTime.Period[BasicTime.earliestGMT, BasicTime.Now[]];
kicks ← 0;
processorID ← ThisMachine.ProcessorID[$Hex];
counter ← CheckCounter[counter, processorID];
userName ← UserCredentials.Get[].name;
UserCredentials.RegisterForChange[NewUser];
};
CheckCounter: PROC [tentativeCounter: INT, pid: ROPE] RETURNS [guaranteedCounter: INT] ~ {
This routine should check pid#tentativeCounter in the scriptDB and the scriptEntriesDB to make sure that all greater UIDs have a different pid. If not, they should return a new counter that satisfies that property. Very unlikely to be needed.
};
Initializations
InitUniqueID[];
END.
Doug Terry, June 3, 1986 4:19:14 pm PDT
Extracted database operations from VoiceRopeImpl.mesa.
changes to: VoiceRopeDBImpl , EXPORTS , ~ , WriteVoiceRope 
Doug Terry, June 3, 1986 5:23:23 pm PDT
changes to: VoiceRopeDBImpl, EXPORTS, ~, Open, WriteVoiceRope, END, DIRECTORY, IMPORTS, Read, SimpleTuneList, NextTuneOnList, TuneListInterval, EntryToTuneList
Doug Terry, June 5, 1986 11:47:40 am PDT
changes to: Open, Read, Write, EntryToTuneList, Error, DIRECTORY
Doug Terry, June 5, 1986 3:07:30 pm PDT
changes to: EntryToTuneList, TuneListInterval
Doug Terry, June 10, 1986 5:55:22 pm PDT
changes to: AddInterest, DropInterest, LookupInterest, Write, Delete, ~, LookupVoiceRope, LookupTune
Doug Terry, June 11, 1986 3:40:47 pm PDT
changes to: LookupTune, Enumerate, LookupVoiceRope, EnumerateInterests, ParseInterestEntry, AddInterest, DropInterest, LookupInterest, CRQuery, RCRQuery
Doug Terry, June 12, 1986 4:45:50 pm PDT
changes to: Open, ReadVoiceRope, EntryToTuneList, ~, WriteVoiceRope, PackHeader, ReplaceID, UnpackHeader, PackInterest, UnpackInterest, DeleteVoiceRope, VoiceRopeContainingTune, EnumerateVoiceRopes, AddInterest, DropInterest, ReplaceInterestID, ReadInterest, InterestForRef, InterestInVoiceRope, EnumerateInterestClass, BadUniqueID, ParseUniqueID, DIRECTORY
Doug Terry, June 17, 1986 12:02:00 pm PDT
changes to: UnpackHeader
Doug Terry, June 26, 1986 2:08:18 pm PDT
changes to: TuneListInterval
Doug Terry, June 26, 1986 5:03:38 pm PDT
changes to: Open, DIRECTORY, IMPORTS
Doug Terry, June 30, 1986 1:11:17 pm PDT
changes to: PackInterest
Doug Terry, June 30, 1986 2:36:30 pm PDT
changes to: UnpackHeader, Open, ReadVoiceRope, WriteVoiceRope, DeleteVoiceRope, VoiceRopeContainingTune, EnumerateVoiceRopes, AddInterest, DropInterest, InterestForRef, InterestInVoiceRope, EnumerateInterestClass, NextTuneOnList, EntryToTuneList, UnpackInterest
Polle Zellweger (PTZ) January 8, 1987 3:56:18 pm PST
changes to: DIRECTORY, ~, ReadScriptDesc, WriteScriptDesc, DeleteScriptDesc, VoiceRopeContainingTune, FindScriptsInFile, ScriptDescToLBEntry
Polle Zellweger (PTZ) January 9, 1987 12:07:17 pm PST
changes to: FindScriptsStartingInFile
Polle Zellweger (PTZ) January 9, 1987 5:07:47 pm PST
changes to: processorID, InitUniqueID, CheckCounter, processorID, counter, GenerateUniqueID, ParseUniqueID, ScriptDescToLBEntry, LBEntryToScriptDesc, NextTuneOnList
Polle Zellweger (PTZ) January 14, 1987 9:58:23 pm PST
changes to: ScriptDescToLBEntry, LBEntryToScriptDesc, ScriptEntryDescToLBEntry, NextTuneOnList
Polle Zellweger (PTZ) January 20, 1987 7:18:32 pm PST
changes to: ScriptEntryDescToLBEntry, LBEntryToScriptDesc, ParseNextEntries, FindScriptsStartingInFile, ReadScriptEntryDesc, WriteScriptEntryDesc, DeleteScriptEntryDesc, FlattenNextEntries
Polle Zellweger (PTZ) January 21, 1987 5:45:54 pm PST
changes to: FlattenNextEntries
Polle Zellweger (PTZ) January 28, 1987 6:55:22 pm PST
changes to: Open, FindScriptsStartingInFile, ReadScriptEntryDesc, ScriptDescToLBEntry, LBEntryToScriptDesc, ScriptEntryDescToLBEntry, LBEntryToScriptEntryDesc, DIRECTORY