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 |
| public final class MemFiler extends SimpleConfigurable implements Filer { |
44 |
| private Map hashTable = null; |
45 |
| private boolean opened = false; |
46 |
| private boolean readOnly = false; |
47 |
| |
48 |
19
| public MemFiler() {
|
49 |
19
| hashTable = Collections.synchronizedMap(new HashMap());
|
50 |
| } |
51 |
| |
52 |
0
| public MemFiler(Map hashTable, boolean readOnly) {
|
53 |
0
| this.hashTable = hashTable;
|
54 |
0
| this.readOnly = readOnly;
|
55 |
| } |
56 |
| |
57 |
0
| public MemFiler(Map hashTable) {
|
58 |
0
| this(hashTable, false);
|
59 |
| } |
60 |
| |
61 |
19
| public void setLocation(File root, String location) {
|
62 |
| } |
63 |
| |
64 |
19
| public String getName() {
|
65 |
19
| return "MemFiler";
|
66 |
| } |
67 |
| |
68 |
6053
| private void checkOpened() throws DBException {
|
69 |
6056
| if (!opened) {
|
70 |
0
| throw new FilerException(FaultCodes.COL_COLLECTION_CLOSED, "Filer is closed");
|
71 |
| } |
72 |
| } |
73 |
| |
74 |
4027
| private void checkReadOnly() throws DBException {
|
75 |
4027
| if (readOnly) {
|
76 |
0
| throw new FilerException(FaultCodes.COL_COLLECTION_READ_ONLY, "Filer is read-only");
|
77 |
| } |
78 |
| } |
79 |
| |
80 |
0
| public boolean create() {
|
81 |
0
| hashTable.clear();
|
82 |
0
| return true;
|
83 |
| } |
84 |
| |
85 |
19
| public boolean open() {
|
86 |
19
| opened = true;
|
87 |
19
| return opened;
|
88 |
| } |
89 |
| |
90 |
0
| public boolean isOpened() {
|
91 |
0
| return opened;
|
92 |
| } |
93 |
| |
94 |
19
| public boolean exists() {
|
95 |
19
| return true;
|
96 |
| } |
97 |
| |
98 |
0
| public boolean drop() {
|
99 |
0
| hashTable.clear();
|
100 |
0
| opened = false;
|
101 |
0
| return !opened;
|
102 |
| } |
103 |
| |
104 |
19
| public boolean close() {
|
105 |
19
| opened = false;
|
106 |
19
| return !opened;
|
107 |
| } |
108 |
| |
109 |
1
| public void flush() {
|
110 |
| } |
111 |
| |
112 |
0
| public Record readRecord(Key key) throws DBException {
|
113 |
0
| return readRecord(key, false);
|
114 |
| } |
115 |
| |
116 |
1959
| public Record readRecord(Key key, boolean metaOnly) throws DBException {
|
117 |
1959
| if (key == null || key.getLength() == 0) {
|
118 |
2
| return null;
|
119 |
| } |
120 |
1957
| checkOpened();
|
121 |
1957
| return (Record) hashTable.get(key);
|
122 |
| } |
123 |
| |
124 |
3017
| public Record writeRecord(Key key, Value value) throws DBException {
|
125 |
3017
| if (key == null || key.getLength() == 0) {
|
126 |
2
| throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Invalid key: '" + key + "'");
|
127 |
| } |
128 |
3018
| if (value == null) {
|
129 |
1
| throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Invalid null value");
|
130 |
| } |
131 |
3015
| checkOpened();
|
132 |
3016
| checkReadOnly();
|
133 |
3017
| hashTable.put(key, new Record(key, value));
|
134 |
3017
| return new Record(key, value);
|
135 |
| } |
136 |
| |
137 |
1012
| public boolean deleteRecord(Key key) throws DBException {
|
138 |
1012
| if (key == null || key.getLength() == 0) {
|
139 |
2
| return false;
|
140 |
| } |
141 |
1010
| checkOpened();
|
142 |
1010
| checkReadOnly();
|
143 |
1010
| return hashTable.remove(key) != null;
|
144 |
| } |
145 |
| |
146 |
46
| public long getRecordCount() throws DBException {
|
147 |
46
| checkOpened();
|
148 |
46
| return hashTable.size();
|
149 |
| } |
150 |
| |
151 |
23
| public RecordSet getRecordSet() throws DBException {
|
152 |
23
| checkOpened();
|
153 |
23
| return new MemRecordSet();
|
154 |
| } |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| private class MemRecordSet implements RecordSet { |
162 |
| private Iterator iter = hashTable.values().iterator(); |
163 |
| |
164 |
26
| public synchronized boolean hasMoreRecords() throws DBException {
|
165 |
26
| return iter.hasNext();
|
166 |
| } |
167 |
| |
168 |
4
| public synchronized Record getNextRecord() throws DBException {
|
169 |
4
| checkOpened();
|
170 |
4
| return (Record) iter.next();
|
171 |
| } |
172 |
| |
173 |
0
| public synchronized Value getNextValue() throws DBException {
|
174 |
0
| checkOpened();
|
175 |
0
| return ((Record) iter.next()).getValue();
|
176 |
| } |
177 |
| |
178 |
0
| public synchronized Key getNextKey() {
|
179 |
0
| return ((Record) iter.next()).getKey();
|
180 |
| } |
181 |
| } |
182 |
| } |
183 |
| |
184 |
| |