starting to test object store
[mir.git] / source / mir / storage / store / StoreContainer.java
1 package mir.storage.store;
2
3 /**
4  * Title:         StoreContainer
5  *
6  * Description:   This is the bucket object for one type of StorableObjects,
7  *                mainy a linked list of StoreIdenfiers. On use or creation
8  *                an object stored in StoreIdentifier is put to head of the
9  *                list. if maximum size of the list is reached, the
10  *                StoreIdentifier at the end of the list is released.
11  *
12  * Copyright:     Copyright (c) 2002
13  * Company:       indy
14  * @author        //rk
15  * @version 1.0
16  */
17
18 import java.util.*;
19 import mir.misc.Logfile;
20
21 public class StoreContainer {
22
23         private final static int    DEFAULT_SIZE=10;
24         private static Logfile      storeLog;
25         private static int          uniqueCounter=10000;
26
27         private LinkedList          container;
28         private StoreContainerType  stocType;
29         private int                 maxSize=DEFAULT_SIZE;
30         private int                 uniqueId;
31
32         private StoreContainer() {};
33
34         public StoreContainer(StoreContainerType stoc_type) {
35                 this.uniqueId=++uniqueCounter;
36                 this.stocType=stoc_type;
37                 this.container=new LinkedList();
38         }
39
40         public StoreContainer(StoreContainerType stoc_type, int maxSize) {
41                 this();
42                 this.maxSize=maxSize;
43         }
44
45         public StorableObject use(StoreIdentifier sid) {
46                 // find sid in LinkedList or die
47                 // move sid to head of linked list
48                 // return reference on object
49                 return null;
50         }
51
52         public boolean has(StoreIdentifier sid) {
53                 return true; // yes yes
54         }
55
56         public void add(StoreIdentifier sid) {
57                 if ( sid != null && sid.hasReference() ) {
58                         //if ( has(sid) )
59                         //      moveToHead(sid);
60                         //else
61                                 container.addFirst(sid);
62                 }
63                 // add to head of linkedlist, if size is exceded throw away tail until
64                 // size ok.
65         }
66
67         /**
68          *  Method:       invalidate(StorableObject sto)
69          *  Description:  finds @see StorableObject, propagates invalidation to
70          *                @see StoreIdentifier and removes StoreIdentifier from
71          *                list.
72          */
73         public void invalidate(StorableObject sto) {
74                 if (sto!=null) {
75                         StoreIdentifier sid = sto.getStoreIdentifier();
76                         if (sid!=null) {
77                                 if ( container.contains(sid) ) {
78                                         sid.invalidate();
79                                         container.remove(sid);
80                                 }
81                         }
82                 }
83         }
84
85         /**
86          *  Method:       setSize
87          *  Description:  readjusts StoreContainer size to value.
88          *
89          */
90         public void setSize(int size) {
91                 if (size <0) return;
92     shrinkToSize(size);
93                 this.maxSize=size;
94         }
95
96         private void shrink() {
97                 shrinkToSize(maxSize);
98         }
99
100   private void shrinkToSize(int size) {
101     if ( size<maxSize && size < container.size() ) {
102                         // shrink
103                         while (size < container.size() ) {
104                                 StoreIdentifier sid = (StoreIdentifier)container.getLast();
105                                 sid.release();
106                                 container.remove(sid);
107                         }
108                 }
109   }
110
111         /**
112          *  Method:       toString()
113          *  Description:  gives out statistical Information, viewable via
114          *                @see ServletStoreInfo.
115          *
116          *  @return       String
117          */
118         public String toString() {
119                 StringBuffer sb = new StringBuffer("StoreContainer id: ");
120                 sb.append(uniqueId).append(" for ");
121                 sb.append(stocType.toString()).append(" // Current/Max size: ");
122                 sb.append(container.size()).append(" / ");
123                 sb.append(maxSize).append("\n");
124                 /** @todo list members ? */
125                 return sb.toString();
126         }
127
128 }