1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.core.filer; |
21 |
| |
22 |
| import org.apache.xindice.core.DBException; |
23 |
| import org.apache.xindice.core.FaultCodes; |
24 |
| import org.apache.xindice.core.data.Key; |
25 |
| import org.apache.xindice.core.data.Record; |
26 |
| import org.apache.xindice.core.data.RecordSet; |
27 |
| import org.apache.xindice.core.data.Value; |
28 |
| import org.apache.xindice.util.SimpleConfigurable; |
29 |
| |
30 |
| import java.io.File; |
31 |
| import java.util.Collections; |
32 |
| import java.util.HashMap; |
33 |
| import java.util.Iterator; |
34 |
| import java.util.Map; |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| public final class SizeableMemFiler extends SimpleConfigurable implements Filer { |
45 |
| |
46 |
| public static final String INITIAL_SIZE_KEY = "org.apache.xindice.core.filer.SizeableMemFiler.INITIAL_SIZE"; |
47 |
| public static final int INITIAL_SIZE = 89; |
48 |
| |
49 |
| private Map hashTable = null; |
50 |
| private boolean opened = false; |
51 |
| private boolean readOnly = false; |
52 |
| |
53 |
| |
54 |
0
| public SizeableMemFiler() {
|
55 |
0
| String aSizeString = System.getProperty(INITIAL_SIZE_KEY);
|
56 |
0
| int aSize = INITIAL_SIZE;
|
57 |
0
| try {
|
58 |
0
| if (aSizeString != null) {
|
59 |
0
| aSize = Integer.parseInt(aSizeString);
|
60 |
| } |
61 |
| } catch (Exception anException) { |
62 |
0
| aSize = INITIAL_SIZE;
|
63 |
| } |
64 |
0
| hashTable = Collections.synchronizedMap(new HashMap(aSize));
|
65 |
| } |
66 |
| |
67 |
0
| public SizeableMemFiler(Map hashTable, boolean readOnly) {
|
68 |
0
| this.hashTable = hashTable;
|
69 |
0
| this.readOnly = readOnly;
|
70 |
| } |
71 |
| |
72 |
0
| public SizeableMemFiler(Map hashTable) {
|
73 |
0
| this(hashTable, false);
|
74 |
| } |
75 |
| |
76 |
0
| public void setLocation(File root, String location) {
|
77 |
| } |
78 |
| |
79 |
0
| public String getName() {
|
80 |
0
| return "SizeableMemFiler";
|
81 |
| } |
82 |
| |
83 |
0
| private void checkOpened() throws DBException {
|
84 |
0
| if (!opened) {
|
85 |
0
| throw new FilerException(FaultCodes.COL_COLLECTION_CLOSED, "Filer is closed");
|
86 |
| } |
87 |
| } |
88 |
| |
89 |
0
| private void checkReadOnly() throws DBException {
|
90 |
0
| if (readOnly) {
|
91 |
0
| throw new FilerException(FaultCodes.COL_COLLECTION_READ_ONLY, "Filer is read-only");
|
92 |
| } |
93 |
| } |
94 |
| |
95 |
0
| public boolean create() {
|
96 |
0
| hashTable.clear();
|
97 |
0
| return true;
|
98 |
| } |
99 |
| |
100 |
0
| public boolean open() {
|
101 |
0
| opened = true;
|
102 |
0
| return opened;
|
103 |
| } |
104 |
| |
105 |
0
| public boolean isOpened() {
|
106 |
0
| return opened;
|
107 |
| } |
108 |
| |
109 |
0
| public boolean exists() {
|
110 |
0
| return true;
|
111 |
| } |
112 |
| |
113 |
0
| public boolean drop() {
|
114 |
0
| hashTable.clear();
|
115 |
0
| opened = false;
|
116 |
0
| return !opened;
|
117 |
| } |
118 |
| |
119 |
0
| public boolean close() {
|
120 |
0
| opened = false;
|
121 |
0
| return !opened;
|
122 |
| } |
123 |
| |
124 |
0
| public void flush() {
|
125 |
| } |
126 |
| |
127 |
0
| public Record readRecord(Key key) throws DBException {
|
128 |
0
| return readRecord(key, false);
|
129 |
| } |
130 |
| |
131 |
0
| public Record readRecord(Key key, boolean metaOnly) throws DBException {
|
132 |
0
| if (key == null || key.getLength() == 0) {
|
133 |
0
| return null;
|
134 |
| } |
135 |
0
| checkOpened();
|
136 |
0
| return (Record) hashTable.get(key);
|
137 |
| } |
138 |
| |
139 |
0
| public Record writeRecord(Key key, Value value) throws DBException {
|
140 |
0
| if (key == null || key.getLength() == 0) {
|
141 |
0
| throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Invalid key: '" + key + "'");
|
142 |
| } |
143 |
0
| if (value == null) {
|
144 |
0
| throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Invalid null value");
|
145 |
| } |
146 |
0
| checkOpened();
|
147 |
0
| checkReadOnly();
|
148 |
0
| hashTable.put(key, new Record(key, value));
|
149 |
0
| return new Record(key, value);
|
150 |
| } |
151 |
| |
152 |
0
| public boolean deleteRecord(Key key) throws DBException {
|
153 |
0
| if (key == null || key.getLength() == 0) {
|
154 |
0
| return false;
|
155 |
| } |
156 |
0
| checkOpened();
|
157 |
0
| checkReadOnly();
|
158 |
0
| return hashTable.remove(key) != null;
|
159 |
| } |
160 |
| |
161 |
0
| public long getRecordCount() throws DBException {
|
162 |
0
| checkOpened();
|
163 |
0
| return hashTable.size();
|
164 |
| } |
165 |
| |
166 |
0
| public RecordSet getRecordSet() throws DBException {
|
167 |
0
| checkOpened();
|
168 |
0
| return new MemRecordSet();
|
169 |
| } |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| private class MemRecordSet implements RecordSet { |
176 |
| private Iterator iter = hashTable.values().iterator(); |
177 |
| |
178 |
0
| public synchronized boolean hasMoreRecords() throws DBException {
|
179 |
0
| return iter.hasNext();
|
180 |
| } |
181 |
| |
182 |
0
| public synchronized Record getNextRecord() throws DBException {
|
183 |
0
| checkOpened();
|
184 |
0
| return (Record) iter.next();
|
185 |
| } |
186 |
| |
187 |
0
| public synchronized Value getNextValue() throws DBException {
|
188 |
0
| checkOpened();
|
189 |
0
| return ((Record) iter.next()).getValue();
|
190 |
| } |
191 |
| |
192 |
0
| public synchronized Key getNextKey() {
|
193 |
0
| return ((Record) iter.next()).getKey();
|
194 |
| } |
195 |
| } |
196 |
| } |