/
IdStore.java
371 lines (327 loc) · 11.4 KB
/
IdStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright 2005--2008 Helsinki Institute for Information Technology
*
* This file is a part of Fuego middleware. Fuego middleware is free
* software; you can redistribute it and/or modify it under the terms
* of the MIT license, included as the file MIT-LICENSE in the Fuego
* middleware source distribution. If you did not receive the MIT
* license with the distribution, write to the Fuego Core project at
* fuego-raxs-users@hoslab.cs.helsinki.fi.
*/
package fc.raxs;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import fc.util.IOUtil;
import fc.util.RingBuffer;
import fc.util.Util;
import fc.util.Debug.Time;
import fc.util.log.Log;
import fc.xml.xas.FragmentItem;
import fc.xml.xas.Item;
import fc.xml.xas.ItemTarget;
import fc.xml.xas.ItemTransform;
import fc.xml.xas.ParserSource;
import fc.xml.xas.SerializerTarget;
import fc.xml.xas.StartTag;
import fc.xml.xas.index.Index;
import fc.xml.xas.index.SeekableKXmlSource;
import fc.xml.xas.index.SeekableSource;
import fc.xml.xas.index.Index.Entry;
import fc.xml.xmlr.ChangeBuffer;
import fc.xml.xmlr.Key;
import fc.xml.xmlr.MutableRefTreeImpl;
import fc.xml.xmlr.NodeNotFoundException;
import fc.xml.xmlr.model.KeyIdentificationModel;
import fc.xml.xmlr.model.TreeModel;
import fc.xml.xmlr.xas.UniformXasCodec;
import fc.xml.xmlr.xas.XasRefTree;
public class IdStore extends XasStore {
public IdStore(StoreConfiguration sc ) throws IOException {
this(sc.getStoreFile(), sc.getModel() == null ? XMLR1_ITEM_TREE :
sc.getModel());
}
public IdStore(File f, TreeModel model) {
//assert model.getIdentificationModel() == IdentificationModel.ID_ATTRIBUTE
// : "No implementation for that tree model";
this.f = f;
this.tm = model;
writable = !f.exists() || f.canWrite();
}
/** @inheritDoc
*/
@Override
public ChangeBuffer getChangeBuffer() {
if( !writable )
return null;
return new ChangeBuffer(new MutableRefTreeImpl(t.getRoot().getId()),t);
}
@Override
protected void init(File f) {
if( Measurements.STORE_TIMINGS )
Time.stamp( Measurements.H_STORE_INIT );
try {
ls = new IdSource(f);
} catch (IOException e) {
trap(e);
}
t = new XasRefTree(ls,(UniformXasCodec) tm.getCodec());
Log.debug("Tree root is "+t.getRoot());
if( Measurements.STORE_TIMINGS )
Log.debug("Init took",Time.sinceFmt(Measurements.H_STORE_INIT));
}
@Override
protected ItemTransform getOutTransform() {
return new LazyTreeConcat();
}
protected static class LazyTreeConcat implements ItemTransform {
RingBuffer<Item> q = new RingBuffer<Item>(2);
Entry start = null;
SeekableSource source = null;
int len = -1, ltc=0;
public boolean hasItems() {
return !q.isEmpty();
}
public Item next() throws IOException {
assert hasItems();
return q.poll();
}
public boolean concatenates(Entry start, SeekableSource source,
LazyTree tree) {
boolean concat = start.getOffset() + len == tree.getEntry().getOffset() &&
tree.getEntry().getContext() == start.getContext();
/*Log.debug("Concat of "+
new Entry(start.getOffset(),len,start.getContext())+" and "+
tree.entry+" is "+concat);*/
return concat;
}
public void append(Item item) throws IOException {
// Emit any in-progress if needed
LazyTree lt = item instanceof LazyTree ?
(LazyTree) item : null;
boolean concatenates = lt != null && start != null
&& concatenates(start,source,lt);
if( start != null && (lt==null || !concatenates ) ) {
// Is in-progress, emit if !lt or non-concat lt
LazyTree lte = new LazyTree(new Entry(start.getOffset(),len,
start.getContext()),source);
//Log.debug("Emitting LazyTree of length "+lte.entry.getLength()+
// " (combines "+ltc+" trees)");
q.offer(lte);
start = null;
}
if( lt != null ) {
// Lazy tree -- concatenate or start
if( start != null ) {
// Concat
assert concatenates : "Non-concat should already have been emitted";
len += lt.getEntry().getLength();
ltc++;
} else {
// Start
start = lt.getEntry();
len = start.getLength();
source = lt.getSource();
ltc=1;
}
} else {
assert start == null : "In progress should be impossible here";
// Ordinary item
q.offer(item);
}
}
}
protected static class LazyTree extends FragmentItem {
private Index.Entry entry;
private SeekableSource source;
public static final int LAZY_TREE = 0x00194200;
public LazyTree(Index.Entry e, SeekableSource s) {
super(LAZY_TREE, 1);
this.entry = e;
this.source = s;
}
public void appendTo (ItemTarget target) throws IOException {
if ( !(target instanceof SerializerTarget) ||
!(source instanceof ParserSource)) {
source.setPosition(entry.getOffset(), entry.getContext());
int end = entry.getOffset()+entry.getLength();
Log.debug("Item-streaming..");
while(source.getCurrentPosition()<end) {
Item i = source.next();
//Log.debug("... "+i);
target.append(i);
}
} else {
ParserSource ps = (ParserSource) source;
SerializerTarget st = (SerializerTarget) target;
source.setPosition(entry.getOffset(), entry.getContext());
if( !Util.equals(ps.getEncoding(), st.getEncoding()))
throw new IOException("Incompatible stream character encodings: "
+ps.getEncoding() +" and "+ st.getEncoding() );
//st.flush(); // Obsolete since patch-397
//Time.stamp();
OutputStream out = st.getOutputStream();
IOUtil.copyStream(ps.getInputStream(), out , entry.getLength());
//Log.info("Stream copy of "+entry.getLength()+" took",Time.sinceFmt());
//out.flush();
/*{
// DEBUG
source.setPosition(entry.getOffset(), entry.getContext());
Log.debug("Copying stream of size "+entry.getLength());
IOUtil.copyStream(ps.getInputStream(), System.out, entry
.getLength());
System.out.flush();
}*/
source.setPosition(entry.getEnd(), entry.getContext());
}
}
public Index.Entry getEntry() {
return entry;
}
public SeekableSource getSource() {
return source;
}
}
protected class IdSource extends LazySource {
private SeekableSource source;
private Map<Key,Entry> index=new HashMap<Key,Entry>();
private Map<Key,Key> parents = new HashMap<Key,Key>();
private Map<Key,List<Key>> childLists = new HashMap<Key,List<Key>>();
private Key root = null;
public IdSource(File f) throws IOException {
init(f);
}
@Override
Item getLazyTree(Key k) throws NodeNotFoundException, IOException {
Entry e = lookupEx(k);
return new LazyTree(e,source);
}
public Key getRoot() {
return root;
}
public Key getParent(Key k) throws NodeNotFoundException {
if( !parents.containsKey(k) )
throw new NodeNotFoundException(k);
return parents.get(k);
}
public boolean contains(Key k) {
return index.containsKey(k);
}
public Iterator<Key> getChildKeys(Key k) throws NodeNotFoundException {
List<Key> cl = childLists.get(k);
if( cl == null )
throw new NodeNotFoundException(k);
return cl.iterator();
}
public void seek(Key k) throws NodeNotFoundException {
Entry e = lookupEx(k);
try {
source.setPosition(e.getOffset(), e.getContext());
} catch( IOException ex) {
throw new NodeNotFoundException("Cannot re-position parser to ",k);
}
}
public KeyIdentificationModel getKeyIdentificationModel() {
return tm;
}
public void close() throws IOException {
if( source != null )
source.close();
}
public Item next() throws IOException {
return source.next();
}
protected Entry lookup(Key k) {
return index.get(k);
}
protected Entry lookupEx(Key k) throws NodeNotFoundException {
Entry e = index.get(k);
if( k == null )
throw new NodeNotFoundException(k);
return e;
}
protected void init(File f) throws IOException {
if( f.length() == 0 ) {
root = null;
return;
}
source = new SeekableKXmlSource(f.getPath());
Stack<ArrayList<Key>> children = new Stack<ArrayList<Key>>() ;
children.push(new ArrayList<Key>()); // Avoid many null checks
Stack<Key> parentStack = new Stack<Key>();
parentStack.push(null);
StartTag context = null;
Stack<StartTag> sts = new Stack<StartTag>();
sts.push(null);
Stack<Integer> ps = new Stack<Integer>();
boolean isText = false;
int depth = Integer.MAX_VALUE;
for (Item item;(item = source.next()) != null;) {
Key k = tm.identify(item);
//Log.debug("Item "+item+", key "+k+" of type "+(k != null ? k.getClass() : ""));
root = root == null && k!=null ? k : root;
if (isText && !Item.isContent(item)) {
Integer pos = ps.pop();
if( k != null ) {
children.peek().add(k);
parents.put(k, parentStack.peek());
addIndexEntry(k, pos, source.getPreviousPosition(), context );
}
isText = false;
}
if (Item.isStartTag(item)) {
if( k != null )
children.peek().add(k);
children.push(new ArrayList<Key>());
parentStack.push(k);
context = (StartTag) item;
sts.push(context);
ps.push(source.getPreviousPosition());
} else if (Item.isEndTag(item)) {
if( parentStack.peek() != null ) {
children.peek().trimToSize();
childLists.put(parentStack.peek(), children.pop());
} else
children.pop();
sts.pop();
context = sts.peek();
Integer pos = ps.pop();
Key thisKey = parentStack.peek();
parentStack.pop();
if (depth >= sts.size() ) {
addIndexEntry(thisKey, pos, source.getCurrentPosition(), context);
parents.put( thisKey, parentStack.peek() );
}
} else if (Item.isContent(item)) {
if (!isText) {
ps.push(source.getPreviousPosition());
}
isText = true;
} else if (Item.isDocumentDelimiter(item)) {
isText = false;
continue;
} else if( k != null ) {
children.peek().add(k);
parents.put(k, parentStack.peek());
addIndexEntry(k, source.getPreviousPosition(),
source.getCurrentPosition(), context );
}
}
Log.debug("Build index of size "+index.size());
//Log.debug("Index entries are",index);
//Log.debug("Parent index is",parents);
//Log.debug("Child-list index is",childLists);
}
private void addIndexEntry(Key k, int start, int end, StartTag context) {
index.put(k, new Entry(start,end-start,context));
}
}
}
// arch-tag: eccd6c61-006d-42c7-946c-e5a51aa4346b
//