001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Core License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: DeathObjectBuffer.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $
008:
009: package org.ozoneDB.core.classicStore;
010:
011: import org.ozoneDB.core.*;
012: import org.ozoneDB.util.*;
013: import org.ozoneDB.DxLib.*;
014:
015: public class DeathObjectBuffer extends Object {
016:
017: /** all objects ordered by their ObjectID */
018: public DxMap objects;
019:
020: /** head and tail of the double linked list */
021: DeathObject head;
022: DeathObject tail;
023:
024: int bufferSize = 0;
025:
026: /** the constructor */
027: public DeathObjectBuffer() {
028: objects = new DxHashMap();
029: head = new DeathObject();
030: tail = new DeathObject();
031: head.previous = tail;
032: tail.next = head;
033: }
034:
035: /** returns the current size of the buffer */
036: public final int size() {
037: return bufferSize;
038: }
039:
040: /** returns the current count of objects of the buffer */
041: public final int count() {
042: return objects.count();
043: }
044:
045: /**
046: */
047: public void updateSize(DeathObject obj, boolean sub) {
048: bufferSize += sub ? -obj.size() : obj.size();
049: }
050:
051: /**
052: * adds the object to the bottom of the stack
053: */
054: public boolean add(DeathObject obj) {
055: boolean result = objects.addForKey(obj, obj.objID());
056: if (result) {
057: popOnTop(obj);
058: bufferSize += obj.size();
059: }
060: return result;
061: }
062:
063: /**
064: * removes the object from the stack
065: */
066: public DeathObject remove(ObjectID oid) {
067: //env.logWriter.newEntry ("DeathObjectBuffer.remove: " + oid, LogWriter.DEBUG3);
068: DeathObject obj = (DeathObject) objects.removeForKey(oid);
069: if (obj != null) {
070: removeFromStack(obj);
071: bufferSize -= obj.size();
072: }
073: return obj;
074: }
075:
076: /**
077: * moves the specified object to the top of the stack
078: */
079: public boolean moveToTop(ObjectID oid) {
080: DeathObject obj = (DeathObject) objects.elementForKey(oid);
081: if (obj != null) {
082: removeFromStack(obj);
083: popOnTop(obj);
084: }
085: return obj != null;
086: }
087:
088: /**
089: * removes and returns the object at the bottom
090: */
091: public DeathObject pushFromBottom() {
092: DeathObject obj = tail.next;
093: if (obj == head) {
094: return null;
095: }
096: return remove(obj.objID());
097: }
098:
099: /**
100: * returns the DeathObject for the specified ObjectID
101: */
102: public DeathObject objectForId(ObjectID oid) {
103: return (DeathObject) objects.elementForKey(oid);
104: }
105:
106: /**
107: * pops the object on the top of the stack
108: */
109: private void popOnTop(DeathObject obj) {
110: head.previous.next = obj;
111: obj.previous = head.previous;
112: obj.next = head;
113: head.previous = obj;
114: }
115:
116: /**
117: * pops the object on the bottom of the stack
118: */
119: private void popOnBottom(DeathObject obj) {
120: tail.next.previous = obj;
121: obj.next = tail.next;
122: obj.previous = tail;
123: tail.next = obj;
124: }
125:
126: /**
127: * removes the object from the stack
128: */
129: private void removeFromStack(DeathObject obj) {
130: obj.previous.next = obj.next;
131: obj.next.previous = obj.previous;
132: }
133: }
|