Information about the Walnut database
ReadOnly:
PUBLIC ENTRY PROC
RETURNS[readonly:
BOOL] = {
Is the database readonly?
ENABLE UNWIND => NULL;
WalnutOpsInternal.CheckInProgress[];
readonly ← WalnutDB.IsReadOnly[]; -- doesn't really read DB
};
GetRootInfo:
PUBLIC ENTRY PROC
RETURNS[createDate: BasicTime.
GMT, rootFile, mailFor:
ROPE] = {
What is the name of the file storing the database?
ENABLE UNWIND => NULL;
Gri:
PROC =
{ [ createDate, , mailFor] ← WalnutDB.GetRootInfo[] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Gri, FALSE];
rootFile ← WalnutOpsInternal.rootFileName;
};
FileName:
PUBLIC
ENTRY
PROC
RETURNS[fileName:
ROPE] = {
ENABLE UNWIND => NULL;
WalnutOpsInternal.CheckInProgress[];
fileName ← WalnutDB.GetDBName[]; -- doesn't read DB
};
SizeOfDatabase:
PUBLIC ENTRY PROC
RETURNS[messages, msgSets:
INT] = {
ENABLE UNWIND => NULL;
Sd:
PROC =
{ [messages, msgSets] ← WalnutDB.SizeOfDatabase[] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Sd, FALSE];
};
LogLength:
PUBLIC ENTRY PROC
RETURNS[length:
INT] = {
ENABLE UNWIND => NULL;
LLen:
PROC =
{ length ← WalnutLog.LogLength[] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[LLen, FALSE];
};
MsgSetsInfo:
PUBLIC ENTRY PROC
RETURNS[version, num:
INT] = {
ENABLE UNWIND => NULL;
Msv:
PROC =
{ [version, num] ← WalnutDB.MsgSetsInfo[] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Msv, FALSE];
};
Primitive message set operations
CreateMsgSet:
PUBLIC
ENTRY
PROC[name:
ROPE, msDomainVersion: DomainVersion] = {
Create a message set with name msgSet.
the named message set already exists (return with exists TRUE).
ENABLE UNWIND => NULL;
exists: BOOL;
Cme:
INTERNAL PROC = {
exists ← WalnutDB.CreateMsgSet[name, msDomainVersion].existed;
[] ← WalnutLog.CreateMsgSet[name];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Cme, TRUE];
IF ~exists THEN WalnutRegistryPrivate.NotifyForMsgSetEvent[created, name];
};
MsgSetExists:
PUBLIC
ENTRY
PROC[name:
ROPE, msDomainVersion: DomainVersion]
RETURNS[exists:
BOOL, version:
INT] = {
ENABLE UNWIND => NULL;
Mse:
INTERNAL PROC =
{ [exists, version] ← WalnutDB.MsgSetExists[name, msDomainVersion] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Mse, FALSE];
};
SizeOfMsgSet:
PUBLIC ENTRY PROC[name:
ROPE]
RETURNS[messages, version:
INT] = {
ENABLE UNWIND => NULL;
Sms:
INTERNAL PROC =
{ [messages, version] ← WalnutDB.NumInMsgSet[name] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Sms, FALSE];
};
EmptyMsgSet:
PUBLIC ENTRY PROC [msgSet: MsgSet]
RETURNS [someInDeleted:
BOOL] = {
Remove all messages from msgSet. Messages that are no longer in any message set will be added to "Deleted".
msgSet is "Deleted" (do nothing).
messages added to "Deleted" (return with someInDeleted TRUE).
ENABLE UNWIND => NULL;
Ems:
INTERNAL
PROC[inProgress:
BOOL] = {
someInDeleted ← FALSE;
IF WalnutDB.EqMsgSets[msgSet.name, DeletedMsgSetName] THEN RETURN;
IF ~inProgress
THEN {
at: INT;
IF ~WalnutDB.VerifyMsgSet[msgSet] THEN RETURN;
at ← WalnutLog.EmptyMsgSet[msgSet.name].at;
WalnutDB.SetOpInProgressPos[at];
};
someInDeleted ← WalnutDB.EmptyMsgSet[msgSet, CheckReport];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.LongRunningApply[Ems];
};
DestroyMsgSet:
PUBLIC ENTRY PROC [msgSet: MsgSet, msDomainVersion: DomainVersion]
RETURNS [someInDeleted:
BOOL] = {
Remove the specified message set from the database.
msgSet must exist (ignore otherwise).
msgSet is "Active" (same as EmptyMsgSet).
msgSet is "Deleted" (do nothing).
ENABLE UNWIND => NULL;
isActive: BOOL;
Dms:
INTERNAL
PROC[inProgress:
BOOL] = {
someInDeleted ← FALSE;
IF ~inProgress
THEN {
at: INT;
IF ~WalnutDB.VerifyMsgSet[msgSet] THEN RETURN;
WalnutDB.VerifyDomainVersion[msDomainVersion];
at ←
IF isActive
THEN WalnutLog.EmptyMsgSet[msgSet.name].at
ELSE WalnutLog.DestroyMsgSet[msgSet.name].at;
WalnutDB.SetOpInProgressPos[at];
};
IF isActive
THEN someInDeleted ← WalnutDB.EmptyMsgSet[msgSet, CheckReport]
ELSE someInDeleted ←
WalnutDB.DestroyMsgSet[msgSet, msDomainVersion, CheckReport];
};
WalnutOpsInternal.CheckInProgress[];
IF msgSet.name.Equal[DeletedMsgSetName, FALSE] THEN RETURN[FALSE]; -- may not destroy Deleted
isActive ← msgSet.name.Equal[ActiveMsgSetName, FALSE];
WalnutOpsInternal.LongRunningApply[Dms];
IF ~isActive THEN WalnutRegistryPrivate.NotifyForMsgSetEvent[destroyed, msgSet.name];
};
EnumerateMsgs:
PUBLIC
ENTRY
PROC
RETURNS[enum: EnumeratorForMsgs] = {
ENABLE UNWIND => NULL;
thisEnum: REF FromLog EnumeratorForMsgsObject;
Em:
INTERNAL
PROC = {
thisEnum.createDate ← WalnutDB.GetRootFileVersion[];
thisEnum.endPos ← WalnutLog.LogLength[];
};
WalnutOpsInternal.CheckInProgress[];
thisEnum ← NEW[FromLog EnumeratorForMsgsObject];
WalnutOpsInternal.CarefullyApply[Em, FALSE];
RETURN[thisEnum];
};
NextMsg:
PUBLIC
ENTRY
PROC[enum: EnumeratorForMsgs]
RETURNS
[msgID: ROPE, msList: LIST OF ROPE, headers: REF TEXT] = {
ENABLE UNWIND => NULL;
ok: BOOL ← TRUE;
Nm:
INTERNAL
PROC = {
skipped: INT;
isAt: INT;
natLen: NAT;
wle: WalnutLog.LogEntry;
TRUSTED {
WITH thisEnum: enum
SELECT
FROM
FromLog => {
IF thisEnum.createDate # WalnutDB.GetRootFileVersion[]
THEN
ERROR WalnutDefs.Error[$db, $InvalidEnumerator, "Wrong rootfile referenced"];
skipped ← WalnutLog.SetPosition[thisEnum.scanPos];
DO
wle: WalnutLog.LogEntry;
at: INT;
[wle, at] ← WalnutLog.QuickScan[];
IF at >= thisEnum.endPos OR wle = NIL THEN { ok ← FALSE; RETURN };
TRUSTED {
WITH mle: wle
SELECT
FROM
CreateMsg => {
natLen ← mle.headersLen; -- this better not cause an error
msgID ← Rope.FromRefText[mle.msg];
isAt ← WalnutDB.GetMsgEntryPosition[msgID];
IF isAt = -1 OR isAt > mle.entryStart THEN LOOP; -- oops or duplicate
msList ← WalnutDB.GetCategories[msgID];
IF thisEnum.headers =
NIL
OR thisEnum.headers.maxLength < natLen
THEN
thisEnum.headers ← RefText.New[natLen];
WalnutLog.GetRefTextFromLog[mle.entryStart+mle.textOffset, natLen, thisEnum.headers];
headers ← thisEnum.headers;
thisEnum.scanPos ← WalnutLog.NextAt[];
EXIT;
};
ENDCASE => LOOP;
};
ENDLOOP;
};
FromMsgSet => {
valid: BOOL;
[msgID, valid] ← WalnutDB.NextMsgInMsgSet[thisEnum.lazyEnum];
IF NOT valid OR msgID = NIL THEN RETURN;
isAt ← WalnutDB.GetMsgEntryPosition[msgID];
msList ← WalnutDB.GetCategories[msgID];
[] ← WalnutLog.SetPosition[isAt];
wle ← WalnutLog.QuickScan[].le;
TRUSTED {
WITH mle: wle
SELECT
FROM
CreateMsg => {
natLen ← mle.headersLen;
IF thisEnum.headers =
NIL
OR thisEnum.headers.maxLength < natLen
THEN
thisEnum.headers ← RefText.New[natLen];
WalnutLog.GetRefTextFromLog[isAt+mle.textOffset, natLen, thisEnum.headers] }
ENDCASE };
headers ← thisEnum.headers };
ENDCASE =>
ERROR WalnutDefs.Error[$db, $InvalidEnumerator, "Wrong type of enumerator"];
};
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Nm, FALSE];
IF ~ok THEN RETURN[NIL, NIL, NIL];
};
EnumerateMsgsInMsgSet:
PUBLIC
ENTRY
PROC[name:
ROPE]
RETURNS[enum: EnumeratorForMsgs] = {
ENABLE UNWIND => NULL;
Me:
INTERNAL
PROC = {
msgSet: WalnutDB.MsgSet;
version: INT;
exists: BOOL;
[exists, version] ← WalnutDB.MsgSetExists[name, WalnutDefs.dontCareDomainVersion];
IF NOT exists THEN RETURN;
msgSet.name ← name; msgSet.version ← version;
enum ← NEW[EnumeratorForMsgsObject ← [enumerator: FromMsgSet[lazyEnum: WalnutDB.EnumerateMsgsInMsgSet[msgSet]]]] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Me, FALSE];
};
MsgsEnumeration:
PUBLIC
ENTRY
PROC[alphaOrder:
BOOL]
RETURNS[mL:
LIST
OF
ROPE] = {
ENABLE UNWIND => NULL;
Me:
INTERNAL
PROC =
{ mL ← WalnutDB.MsgsEnumeration[alphaOrder] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Me, FALSE];
};
MsgSetNames:
PUBLIC
ENTRY
PROC[alphaOrder:
BOOL]
RETURNS[mL:
LIST
OF
ROPE, msDomainVersion: DomainVersion] = {
Create an enumeration for a message set.
ENABLE UNWIND => NULL;
Ems:
INTERNAL
PROC =
{ [mL, msDomainVersion] ← WalnutDB.MsgSetsNames[alphaOrder] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Ems, FALSE];
};
MsgsInSetEnumeration:
PUBLIC
ENTRY
PROC[name:
ROPE, fromStart:
BOOL]
RETURNS [mL:
LIST
OF
ROPE, msVersion: MsgSetVersion] = {
Create an enumeration for the messages in a message set.
ENABLE UNWIND => NULL;
Emis:
INTERNAL
PROC =
{ [mL, msVersion] ← WalnutDB.MsgsInSetEnumeration[name, fromStart] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Emis, FALSE];
};
EnumerateMsgSets:
PUBLIC
ENTRY
PROC [
alphaOrder:
BOOL ←
TRUE, proc:
PROC[msgSet: MsgSet] ]
RETURNS [msVersion: MsgSetVersion] = {
ENABLE UNWIND => NULL;
Emss:
INTERNAL
PROC =
{ msVersion ← WalnutDB.EnumerateMsgSets[alphaOrder, proc] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Emss, FALSE];
};
EnumerateMsgsInSet:
PUBLIC
ENTRY
PROC [
name:
ROPE,
fromStart:
BOOL,
proc:
PROC[msg, TOCentry:
ROPE, hasBeenRead:
BOOL, startOfSubject:
INT]]
RETURNS [msVersion: MsgSetVersion] = {
ENABLE UNWIND => NULL;
Ems:
INTERNAL
PROC =
{ msVersion ← WalnutDB.EnumerateMsgsInSet[name, fromStart, proc] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Ems, FALSE];
};
Primitive message operations
MsgExists:
PUBLIC
ENTRY
PROC [msg:
ROPE]
RETURNS[exists:
BOOL] = {
ENABLE UNWIND => NULL;
Me:
INTERNAL PROC =
{ exists ← WalnutDB.MsgExists[msg] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Me, FALSE];
};
GetCategories:
PUBLIC
ENTRY
PROC [msg:
ROPE]
RETURNS[msL:
LIST
OF
ROPE] = {
ENABLE UNWIND => NULL;
GetC:
INTERNAL PROC =
{ msL← WalnutDB.GetCategories[msg] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[GetC, FALSE];
};
GetDisplayProps:
PUBLIC
ENTRY
PROC[msg:
ROPE]
RETURNS[hasBeenRead:
BOOL, TOCentry:
ROPE, startOfSubject:
INT] = {
ENABLE UNWIND => NULL;
GetDP:
INTERNAL PROC =
{ [hasBeenRead, TOCentry, startOfSubject] ← WalnutDB.GetDisplayProps[msg] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[GetDP, FALSE];
};
GetMsgDate:
PUBLIC
ENTRY
PROC [msg:
ROPE]
RETURNS[date: BasicTime.
GMT] = {
ENABLE UNWIND => NULL;
GetMD: INTERNAL PROC = { date ← WalnutDB.GetMsgDate[msg] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[GetMD, FALSE];
};
GetMsg:
PUBLIC
ENTRY
PROC [msg:
ROPE]
RETURNS[contents: TiogaContents, herald, shortName:
ROPE] = {
ENABLE UNWIND => NULL;
GetMT:
INTERNAL PROC = {
textStart, textLen, formatLen, shortNameLen: INT;
[textStart, textLen, formatLen, herald, shortNameLen] ← WalnutDB.GetMsgText[msg];
IF textStart = 0 THEN RETURN;
contents ← WalnutLog.GetTiogaContents[textStart, textLen, formatLen];
shortName ← herald.Substr[0, shortNameLen];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[GetMT, FALSE];
};
GetMsgText:
PUBLIC
ENTRY
PROC [msg:
ROPE, text:
REF
TEXT]
RETURNS[contents: REF TEXT] = {
ENABLE UNWIND => NULL;
CheckForNat: PROC[len: INT] RETURNS[nat: NAT] = { nat ← len };
Gmt:
INTERNAL PROC = {
textStart, textLen: INT;
natLen: NAT;
[textStart, textLen, ] ← WalnutDB.GetMsgText[msg];
IF textStart = 0
THEN {
contents ← text;
IF contents # NIL THEN contents.length ← 0;
RETURN};
BEGIN
ENABLE RuntimeError.BoundsFault =>
GOTO oops;
natLen ← CheckForNat[textLen];
IF text =
NIL
OR (natLen > text.maxLength)
THEN
contents ← RefText.New[natLen] ELSE contents ← text;
WalnutLog.GetRefTextFromLog[textStart, textLen, contents];
EXITS
oops =>
ERROR WalnutDefs.Error[$log, $MsgTooLong, "Msg will not fit in a REF TEXT"];
END;
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Gmt, FALSE];
};
GetMsgHeaders:
PUBLIC
ENTRY
PROC [msg:
ROPE, text:
REF
TEXT]
RETURNS[headers: REF TEXT] = {
ENABLE UNWIND => NULL;
natLen: NAT;
Gmh:
INTERNAL PROC = {
wle: WalnutLog.LogEntry;
isAt: INT = WalnutDB.GetMsgEntryPosition[msg];
headers ← text;
IF isAt = -1
THEN {
IF headers # NIL THEN headers.length ← 0;
RETURN};
[] ← WalnutLog.SetPosition[isAt];
[wle, ] ← WalnutLog.QuickScan[];
BEGIN ENABLE RuntimeError.BoundsFault => GOTO oops;
TRUSTED {
WITH mle: wle
SELECT
FROM
CreateMsg => {
natLen: NAT ← mle.headersLen;
IF headers =
NIL
OR headers.maxLength < natLen
THEN
headers ← RefText.New[natLen];
WalnutLog.GetRefTextFromLog[
mle.entryStart+mle.textOffset, natLen, headers];
};
ENDCASE => NULL;
};
EXITS
oops =>
ERROR WalnutDefs.Error[$log, $MsgHeadersTooLong,
IO.PutFR["Msg headers (%g bytes) will not fit in a REF TEXT", IO.int[natLen]]];
END;
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Gmh, FALSE];
};
GetMsgShortName:
PUBLIC
ENTRY
PROC [msg:
ROPE]
RETURNS[shortName:
ROPE] = {
ENABLE UNWIND => NULL;
Gmsn:
INTERNAL PROC = {
shortNameLen: INT;
herald: ROPE;
[, , , herald, shortNameLen] ← WalnutDB.GetMsgText[msg];
shortName ← herald.Substr[0, shortNameLen];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Gmsn, FALSE];
};
GetMsgSize:
PUBLIC
ENTRY
PROC [msg:
ROPE]
RETURNS[textLen, formatLen:
INT] = {
ENABLE UNWIND => NULL;
Gms:
INTERNAL PROC =
{ [, textLen, formatLen] ← WalnutDB.GetMsgTextInfo[msg] };
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Gms, FALSE];
};
GetHasBeenRead:
PUBLIC
ENTRY
PROC [msg:
ROPE]
RETURNS[hadBeenRead:
BOOL] = {
ENABLE UNWIND => NULL;
Ghbr:
INTERNAL
PROC = {
hadBeenRead ← WalnutDB.GetHasBeenRead[msg];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Ghbr, FALSE];
};
SetHasBeenRead:
PUBLIC
ENTRY
PROC [msg:
ROPE] = {
ENABLE UNWIND => NULL;
Shbr:
INTERNAL PROC = {
[] ← WalnutLog.HasBeenRead[msg];
WalnutDB.SetHasBeenRead[msg];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Shbr, TRUE];
WalnutRegistryPrivate.NotifyForMsgEvent[firstRead, msg];
};
More complex message set / message operations
AddMsg:
PUBLIC
ENTRY
PROC [msg:
ROPE, from, to: MsgSet]
RETURNS [exists:
BOOL] = {
Add the message (msg) to the message set (to).
msg must exist (ignore).
to must exist (ignore).
to must not be "Deleted" (ignore).
msg is already in to (return with exists TRUE)
msg had been in "Deleted" (was taken out) (return with undeleted TRUE)
ENABLE UNWIND => NULL;
Am:
INTERNAL PROC = {
exists ← WalnutDB.AddMsg[msg, from, to];
[] ← WalnutLog.AddMsg[msg, to.name];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Am, TRUE];
IF ~exists THEN WalnutRegistryPrivate.NotifyForMove[msg: msg, to: to.name, from: NIL];
};
MoveMsg:
PUBLIC
ENTRY
PROC [msg:
ROPE, from, to: MsgSet]
RETURNS [exists:
BOOL] = {
Move the message (msg) from one message set (from) to another (to).
msg must exist (ignore).
from, to must exist (ignore).
msg must be in from (ignore).
msg is already in to (return with exists TRUE).
msg was in "Deleted" (return with undeleted TRUE)
msg is now in "Deleted" (return with deleted TRUE)
ENABLE UNWIND => NULL;
Mm:
INTERNAL PROC = {
exists ← WalnutDB.MoveMsg[msg, from, to];
[] ← WalnutLog.MoveMsg[msg, from.name, to.name];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Mm, TRUE];
IF ~exists
THEN
WalnutRegistryPrivate.NotifyForMove[msg: msg, to: to.name, from: from.name];
};
RemoveMsg:
PUBLIC
ENTRY
PROC [msg:
ROPE, from: MsgSet, deletedVersion: MsgSetVersion]
RETURNS [deleted:
BOOL] = {
Remove the message (msg) from the message set (from). If this action results in the message not belonging to any message set, it will be placed in the Deleted message set.
msg must exist (ignore).
from must exist (ignore).
from must not be "Deleted" (ignore).
msg must be in from (ignore).
msg was added to "Deleted" (return with nowInDeleted TRUE).
ENABLE UNWIND => NULL;
Rm:
INTERNAL PROC = {
deleted ← WalnutDB.RemoveMsg[msg, from, deletedVersion];
[] ← WalnutLog.RemoveMsg[msg, from.name];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Rm, TRUE];
IF deleted
THEN WalnutRegistryPrivate.NotifyForMsgEvent[deleted, msg]
ELSE WalnutRegistryPrivate.NotifyForMove[msg: msg, to: NIL, from: from.name];
};
Adding new messages from grapevine to the database
used by newMail process
StartNewMail:
PUBLIC
ENTRY
PROC[]
RETURNS[newMailStream:
IO.
STREAM] = {
ENABLE UNWIND => NULL;
newMailLogLength: INT ← -1;
expungeInProgress: BOOL ← FALSE;
Gnml:
PROC = {
newMailLogLength ← WalnutDB.GetNewMailLogLength[];
expungeInProgress ← WalnutDB.GetLogExpungePhase[] # idle;
};
WalnutOpsInternal.CheckInProgress[];
IF WalnutOpsInternal.mailStream # NIL THEN RETURN[NIL];
WalnutOpsInternal.CarefullyApply[proc: Gnml, didUpdate: FALSE];
IF newMailLogLength = -1 OR expungeInProgress THEN RETURN[NIL];
RETURN[WalnutOpsInternal.mailStream ← WalnutMiscLog.GetNewMailLog[newMailLogLength, -1]];
};
RecordNewMailInfo:
PUBLIC
ENTRY PROC[logLen:
INT, server:
ROPE, num:
INT] = {
ENABLE UNWIND => NULL;
when: BasicTime.GMT;
Snml:
PROC = {
now: INT ← WalnutDB.GetServerInfo[server] + num;
[] ← WalnutLog.RecordNewMailInfo[logLen, when ← BasicTime.Now[], server, now];
WalnutDB.SetNewMailInfo[logLen, when, server, now];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[proc: Snml, didUpdate: TRUE];
WalnutOpsInternal.newMailSomewhere ← TRUE;
};
EndNewMail:
PUBLIC
ENTRY PROC = {
ENABLE UNWIND => NULL;
strm: IO.STREAM ← WalnutOpsInternal.mailStream;
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.mailStream ← NIL;
IF strm #
NIL
THEN
WalnutMiscLog.CloseNewMailLog[ ! WalnutDefs.Error , IO.Error, FS.Error => CONTINUE];
IF WalnutOpsInternal.newMailSomewhere
THEN
WalnutRegistryPrivate.NotifyForEvent[mailRead];
};
used by higher levels
GetNewMail:
PUBLIC
ENTRY
PROC[
activeVersion:
INT,
proc:
PROC[msg, TOCentry:
ROPE, startOfSubject:
INT]]
RETURNS[responses:
LIST
OF WalnutOps.ServerInfo, complete:
BOOL] = {
ENABLE UNWIND => NULL;
someEntries: BOOL;
Cml:
PROC = {
IF activeVersion # WalnutDefs.dontCareMsgSetVersion
THEN
[] ← WalnutDB.VerifyMsgSet[ [ActiveMsgSetName, activeVersion] ];
someEntries ← WalnutDB.GetNewMailLogLength[] # 0;
};
Cml2:
PROC[inProgress:
BOOL] = {
fromPos: INT ← 0;
complete ← WalnutLog.PrepareToCopyTempLog[which: newMail, pagesAlreadyCopied: 0, reportProc: WalnutOpsInternal.CheckReport];
IF ~complete THEN RETURN;
IF ~inProgress
THEN {
at: INT ← WalnutLog.StartCopyNewMail[].at;
WalnutDB.SetOpInProgressPos[at];
WalnutDB.SetCopyMailLogPos[at];
}
ELSE {
at: INT = WalnutDB.GetCopyMailLogPos[];
WalnutOpsInternal.CheckReport["\n Continue copying the newMailLog\n"];
[] ← WalnutLog.SetPosition[at];
[] ← WalnutLog.NextEntry[]; -- skip the copy entry
fromPos ← WalnutLog.LogLength[] - WalnutLog.NextAt[];
};
WalnutDB.SetAddingServerMsgs[TRUE];
WalnutLog.CopyTempLog[newMail, WalnutDB.GetCopyMailLogPos[], fromPos, WalnutOpsInternal.CheckReport];
-- raises error if problem
WalnutOpsInternal.CheckReport["\n"];
WalnutDB.SetParseLogInProgress[TRUE];
WalnutDB.SetParseLogPos[WalnutDB.GetOpInProgressPos[]];
WalnutDB.SetOpInProgressPos[-1];
};
Gnm:
PROC = {
IF proc # NIL THEN WalnutDB.EnumerateUnacceptedMsgs[activeVersion, proc];
responses ← WalnutDB.EnumerateServers[];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.CarefullyApply[Cml, FALSE];
IF WalnutOpsInternal.mailStream # NIL THEN RETURN[NIL, FALSE]; -- file is busy
IF someEntries
THEN {
WalnutOpsInternal.LongRunningApply[Cml2];
IF complete
THEN {
WalnutLog.ForgetLogStreams[];
WalnutLog.OpenLogStreams[];
WalnutOpsInternal.CheckReport["Adding new mail to the database\n"];
[] ← WalnutOpsInternal.ParseLog[TRUE]; -- "see" messages
};
}
ELSE complete ← TRUE;
WalnutOpsInternal.CarefullyApply[Gnm, FALSE];
};
AcceptNewMail:
PUBLIC
ENTRY
PROC[activeVersion:
INT] = {
ENABLE UNWIND => NULL;
Anm:
PROC[inProgress:
BOOL] = {
at: INT;
IF ~inProgress
THEN {
[] ← WalnutDB.VerifyMsgSet[[ActiveMsgSetName, activeVersion]];
at ← WalnutLog.AcceptNewMail[].at;
WalnutDB.SetOpInProgressPos[at];
}
ELSE at ← WalnutDB.GetAcceptNewMailPos[];
WalnutDB.AcceptNewMail[at, activeVersion];
};
WalnutOpsInternal.CheckInProgress[];
WalnutOpsInternal.LongRunningApply[Anm];
WalnutOpsInternal.newMailSomewhere ← FALSE;
};
Starting and stopping Walnut
Startup:
PUBLIC
ENTRY
PROC [rootFile:
ROPE, wantReadOnly:
BOOL ←
FALSE]
RETURNS[isReadOnly, newMailExists:
BOOL, mailFor: GVBasics.RName, key:
ROPE] = {
Start up Walnut with a reference to a Walnut log file. This may involve replaying any actions specified in the "tail" of the log (that portion beyond the lastCommit) so that the database and log agree (in fact, if the database does not exist, it may involve reconstruction of the database in its entirety). If fullScavenge is TRUE, the database is emptied and reconstructed using information from the log. If wantReadOnly is TRUE, the log and the database are opened for reading only. If the log or the database cannot be written, readOnly is TRUE upon return.
already started ($AlreadyStarted).
database or log has become inaccessible ($DatabaseInaccessible, $LogInaccessible)
cannot write the log or the database (return with isReadOnly TRUE).
ENABLE {
WalnutDefs.Error => { errorInProgress ← TRUE; REJECT};
UNWIND => NULL;
};
rootFileCreateDate: GMT;
schemaInvalid: BOOL ← TRUE;
InitialCheck:
INTERNAL
PROC = {
rootCreateDate: GMT;
rootFileKey, mailForFromDB: ROPE;
IF key.Length[] # 0
THEN {
[rootCreateDate, rootFileKey, mailForFromDB] ← WalnutDB.GetRootInfo[];
IF rootCreateDate = BasicTime.nullGMT
OR
(rootCreateDate = rootFileCreateDate
AND
rootFileKey.Length[] = 0 AND
mailForFromDB.Length[] = 0)
THEN {
WalnutDB.SetRootInfo[rootFileCreateDate, key, mailFor];
WalnutRoot.CommitAndContinue[];
}
ELSE {
IF ~rootFileKey.Equal[key,
FALSE]
THEN
WalnutDefs.Error[$db, $WrongRootFile,
IO.PutFR["RootFile has key %g, database says %g",
IO.rope[key], IO.rope[rootFileKey]]];
IF ~mailForFromDB.Equal[mailFor,
FALSE]
THEN
WalnutDefs.Error[$db, $WrongRootFile,
IO.PutFR["RootFile says mailFor %g, database says %g",
IO.rope[mailFor], IO.rope[mailForFromDB]]];
IF rootCreateDate # rootFileCreateDate
THEN
WalnutDefs.Error[$db, $WrongRootFile,
IO.PutFR["RootFile has date %g, database says %g",
IO.time[rootFileCreateDate], IO.time[rootCreateDate]]];
};
};
IF (WalnutOpsInternal.newMailSomewhere ← WalnutDB.GetNewMailLogLength[] # 0)
THEN RETURN;
check for unaccepted mail
BEGIN
serverList: LIST OF ServerInfo ← WalnutDB.EnumerateServers[];
FOR sL:
LIST
OF ServerInfo ← serverList, sL.rest
UNTIL sL =
NIL
DO
IF (WalnutOpsInternal.newMailSomewhere ← sL.first.num#0) THEN RETURN;
ENDLOOP;
END;
};
IF started THEN RETURN WITH ERROR WalnutDefs.Error[$db, $AlreadyStarted];
StartStatsReporting[];
[key, mailFor, rootFileCreateDate, walnutSegment, systemIsReadOnly] ←
WalnutRoot.Open[rootName: rootFile, readOnly: wantReadOnly];
rootFileName ← rootFile;
BEGIN
exp: ROPE;
BEGIN
schemaInvalid ← WalnutRoot.StartTransaction[ ! WalnutDefs.Error => {
IF code # $MismatchedSegment THEN REJECT;
exp ← explanation;
GOTO mismatched;
}];
EXITS
mismatched => RETURN WITH ERROR WalnutDefs.Error[$db, $SchemaMismatch, exp];
END;
END;
schemaInvalid ← WalnutRoot.StartTransaction[]
;
WalnutLog.OpenLogStreams[];
walnutRootFile ← rootFile;
isReadOnly ← systemIsReadOnly;
BEGIN
exp: ROPE;
BEGIN
WalnutDB.DeclareDB[walnutSegment, schemaInvalid !
WalnutDefs.SchemaMismatch => { exp ← explanation; GOTO mismatch}];
StatsReport[IO.PutFR["\n\n ***** Startup called with rootFile: %g", IO.rope[rootFile]]];
EXITS
mismatch => RETURN WITH ERROR WalnutDefs.Error[$db, $SchemaMismatch, exp];
END;
END;
started ← TRUE;
errorInProgress ← FALSE;
CarefullyApply[InitialCheck, FALSE];
CheckInProgress[];
newMailExists ← WalnutOpsInternal.newMailSomewhere;
WalnutRegistryPrivate.NotifyForEvent[started];
};
Shutdown:
PUBLIC ENTRY PROC = {
Save the Walnut state and shutdown Walnut.
ENABLE {
WalnutDefs.Error => { errorInProgress ← TRUE; REJECT};
UNWIND => NULL;
};
WalnutRoot.UnregisterStatsProc[StatsReport];
WalnutLogExpunge.ExpShutdown[]; -- clear its variables
WalnutMiscLog.MiscShutdown[]; -- clear its variables
WalnutLog.ShutdownLog[];
WalnutRoot.Shutdown[]; -- takes care of database
StatsReport["\n *** Shutdown"];
IF statsProgressTS # NIL THEN { statsProgressTS.inhibitDestroy ← FALSE };
IF statsStream # NIL THEN { statsStream.Close[]; statsStream ← NIL };
IF statsProgressStream #
NIL
THEN
{ statsProgressStream.Close[]; statsProgressStream ← NIL };
started ← FALSE;
recentActivity← FALSE;
isShutdown ← FALSE;
errorInProgress ← FALSE;
mailStream ← NIL;
WalnutRegistryPrivate.NotifyForEvent[stopped];
};
Scavenge:
PUBLIC
ENTRY
PROC[rootFile:
ROPE]
RETURNS[newMailExists: BOOL, mailFor: GVBasics.RName, key: ROPE] = {
ENABLE {
WalnutDefs.Error => { errorInProgress ← TRUE; REJECT};
UNWIND => NULL;
};
rootFileCreateDate: GMT;
WalnutLog.ShutdownLog[];
WalnutRoot.Shutdown[];
StartStatsReporting[];
StatsReport["\n *** Scavenge"];
[key, mailFor, rootFileCreateDate, walnutSegment, systemIsReadOnly] ←
WalnutRoot.Open[rootName: rootFile, readOnly: FALSE, newSegmentOk: TRUE];
IF systemIsReadOnly
THEN
ERROR WalnutDefs.Error[$DB, $IsReadOnly, "Can't erase a readonly database"];
rootFileName ← rootFile;
[] ← WalnutRoot.StartTransaction[openDB: FALSE]; -- don't care if schema is invalid
WalnutLog.OpenLogStreams[];
WalnutLog.AcquireWriteLock[];
started ← TRUE;
errorInProgress ← FALSE;
WalnutRoot.EraseDB[];
WalnutDB.InitSchema[walnutSegment];
WalnutDB.SetRootInfo[rootFileCreateDate, key, mailFor];
WalnutDB.SetParseLogInProgress[TRUE];
WalnutDB.SetParseLogPos[0];
WalnutRoot.CommitAndContinue[];
WalnutOpsInternal.newMailSomewhere ← FALSE;
[] ← WalnutOpsInternal.ParseLog[TRUE];
WalnutDB.SetTimeOfLastScavenge[BasicTime.Now[]];
newMailExists ← WalnutOpsInternal.newMailSomewhere;
};