-- File: DBIndexHandleImpl.mesa -- Created by: Suzuki, December 8, 1980 2:19 PM -- Last edited by: -- Suzuki, December 8, 1980 3:09 PM -- MBrown, February 27, 1981 5:31 PM -- Cattell, September 15, 1983 4:05 pm -- Willie-Sue on June 30, 1982 4:50 pm DIRECTORY DBCommon, DBStorage, DBStorageInternal, DBIndex, DBIndexHandle, IO; DBIndexHandleImpl: PROGRAM IMPORTS DBCommon, DBStorageInternal, IO EXPORTS DBIndexHandle = { RealIndexHandle: TYPE = DBIndex.RealIndexHandle; IndexObject: TYPE = DBIndex.IndexObject; PriorityList: RealIndexHandle; -- this points to a ring buffer of RealIndexHandle's. They form a LRU list, with PriorityList pointing to the most recently used Index and the next one is PriorityList.back, ...back. If what PriorityList points to is free, then everything on the ring buffer is free. If there is any free Index, PriorityList.fron points to a free Index. FinalizeIndexHandle: PUBLIC PROC = BEGIN i: RealIndexHandle; FOR i _ PriorityList, i.back UNTIL i.back = PriorityList DO IF i.free AND i.depth # 0 THEN ERROR; i.free _ TRUE; i.depth _ 0; ENDLOOP; END; -- IndexHandle manipulation CreateNewRealIndexHandle: PUBLIC PROC [tid: DBStorageInternal.TID] = -- Creates a RealIndexHandle at the head of LRU queue, and set tid. BEGIN q: RealIndexHandle; IF PriorityList.front.free THEN BEGIN PriorityList _ PriorityList.front; GOTO Initialize END ELSE BEGIN q _ NEW[IndexObject]; q.front _ PriorityList.front; q.back _ PriorityList; PriorityList.front.back _ q; PriorityList.front _ q; PriorityList _ q; GOTO Initialize END; EXITS Initialize => { PriorityList^ _ [tid, Segment[tid], DBCommon.NullDBPage, NIL, 0, PriorityList.front, PriorityList.back, FALSE]}; END; GetOldRealIndexHandle: PUBLIC PROC [x: DBStorage.IndexHandle] RETURNS [RealIndexHandle] = -- Returns the actual index handle out of the tuple level's tuple that represents it. BEGIN q: RealIndexHandle _ PriorityList; tid: DBStorageInternal.TID _ DBStorageInternal.TIDOfTuple[x]; DO IF q.free THEN GOTO NotFound; IF q.tid = tid THEN { -- put it at the front of PriorityList IF q = PriorityList THEN GOTO Found; q.front.back _ q.back; q.back.front _ q.front; q.front _ PriorityList.front; PriorityList.front.back _ q; PriorityList.front _ q; q.back _ PriorityList; PriorityList _ q; GOTO Found}; q _ q.back; IF q = PriorityList THEN GOTO NotFound; REPEAT Found => {RETURN[q]}; NotFound => { ttr: DBStorageInternal.TupleTree_ NEW[DBStorageInternal.TupleTreeRecord]; CreateNewRealIndexHandle[tid]; DBStorageInternal.ReadIndexObject[x, ttr]; PriorityList.rootDB _ ttr.rootPA; PriorityList.depth _ ttr.depth; RETURN[PriorityList]}; ENDLOOP; END; DestroyIndexHandle: PUBLIC PROC [q: RealIndexHandle] = -- Deallocates the IndexObject. The real one, not the tuple level's. -- Doesn't actually free any storage to system, just marks it free. BEGIN IF q.front = q THEN -- there is only on item NULL ELSE IF q = PriorityList THEN PriorityList _ PriorityList.back ELSE BEGIN q.front.back _ q.back; q.back.front _ q.front; q.front _ PriorityList.front; q.back _ PriorityList; q.front.back _ q; PriorityList.front _ q END; q.free _ TRUE; END; DeleteHandle: PUBLIC PROC [q: RealIndexHandle] = -- Deallocates the IndexObject BEGIN IF q.front = q THEN -- there is only on item NULL ELSE IF q = PriorityList THEN PriorityList _ PriorityList.back ELSE BEGIN q.front.back _ q.back; q.back.front _ q.front; q.front _ PriorityList.front; q.back _ PriorityList; q.front.back _ q; PriorityList.front _ q END; q.rootDB _ DBCommon.NullDBPage; q.root _ NIL; q.depth _ 0; q.free _ TRUE; END; Segment: PROC [tid: DBStorageInternal.TID] RETURNS [DBCommon.DBPage] = -- Returns the segment in which "tid" exists BEGIN OPEN DBStorageInternal; segment: DBCommon.DBPage; tuple: DBStorage.TupleHandle _ MakeTupleHandle[tid, ]; segment _ SegmentIDOfTuple[tuple]; ReleaseTupleHandle[tuple]; RETURN[segment] END; PrintHandle: PROC = { q: RealIndexHandle _ PriorityList; DO DBCommon.GetDebugStream[].PutF["tid: %12bB, rootDB: %12bB, depth: %bB, %s*n", IO.card[q.tid], IO.card[q.rootDB], IO.card[q.depth], IO.rope[IF q.free THEN "free" ELSE "not free"]]; q _ q.back; IF q=PriorityList THEN EXIT; ENDLOOP; }; PriorityList _ NEW[IndexObject]; PriorityList.front _ PriorityList.back _ PriorityList; PriorityList.free _ TRUE; }. CHANGE LOG Changed by MBrown on December 15, 1980 10:15 AM -- In FinalizeIndexHandle, set i.free _ TRUE in loop. Changed by MBrown on February 27, 1981 5:34 PM -- Allocate from zone. Use CWF instead of IODefs and StringDefs (!) Changed by Cattell sometime in March 1981 -- Removed use of CWF. Changed by Cattell on June 20, 1982 6:23 pm -- Added some comments to procedures. Bad names for procs, should change the interface and recompile everything sometime. Changed by Willie-Sue on June 25, 1982 9:19 am -- IOStream => IO Changed by Willie-Sue on June 30, 1982 4:50 pm -- PrivateIO.DebugStream => DBCommon.GetDebugStream[] Changed by Cattell on August 6, 1982 4:48 pm -- POINTERs to REFs. Changed by Cattell on September 21, 1982 9:20 pm -- Re-organization of DBIndex level.