01: package com.jofti.store;
02:
03: import com.jofti.core.IStoreManager;
04: import com.jofti.exception.JoftiException;
05:
06: import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingQueue;
07:
08: /**
09: * Pool of Externalizable helpers to obtain and release helpers to and from.
10: * <p>
11: * @author xenephon
12: *
13: */
14: public class HelperManager {
15:
16: LinkedBlockingQueue freeQueue = null;
17: IStoreManager manager = null;
18: int nodeSize;
19: int blockSize;
20:
21: /**
22: *
23: */
24: public HelperManager() {
25: }
26:
27: public void init(int helperNumber, int blockSize, int nodeSize,
28: IStoreManager manager) throws JoftiException {
29: // set up the buffer pool
30: freeQueue = new LinkedBlockingQueue(helperNumber);
31: this .manager = null;
32: this .blockSize = blockSize;
33: this .nodeSize = nodeSize;
34: // set up the helpers
35: for (int i = 0; i < helperNumber; i++) {
36: ExternalisableHelper helper = new ExternalisableHelper();
37: helper.init(nodeSize, blockSize, manager);
38: freeQueue.add(helper);
39: }
40: }
41:
42: public ExternalisableHelper acquireHelper() throws JoftiException {
43:
44: ExternalisableHelper helper = null;
45:
46: helper = (ExternalisableHelper) freeQueue.poll();
47: if (helper == null) {
48: helper = new ExternalisableHelper();
49: helper.init(nodeSize, blockSize, manager);
50: }
51: return helper;
52: }
53:
54: public void releaseHelper(ExternalisableHelper helper) {
55: freeQueue.offer(helper);
56: }
57: }
|