01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.sail.nativerdf;
07:
08: import java.io.IOException;
09: import java.util.NoSuchElementException;
10:
11: import info.aduna.io.ByteArrayUtil;
12: import info.aduna.iteration.CloseableIterationBase;
13:
14: import org.openrdf.model.Resource;
15: import org.openrdf.model.Statement;
16: import org.openrdf.model.URI;
17: import org.openrdf.model.Value;
18: import org.openrdf.sail.nativerdf.btree.RecordIterator;
19:
20: /**
21: * A statement iterator that wraps a RecordIterator containing statement records
22: * and translates these records to {@link Statement} objects.
23: */
24: class NativeStatementIterator extends
25: CloseableIterationBase<Statement, IOException> {
26:
27: /*-----------*
28: * Variables *
29: *-----------*/
30:
31: private RecordIterator btreeIter;
32:
33: private ValueStore valueStore;
34:
35: private byte[] nextValue;
36:
37: /*--------------*
38: * Constructors *
39: *--------------*/
40:
41: /**
42: * Creates a new NativeStatementIterator.
43: */
44: public NativeStatementIterator(RecordIterator btreeIter,
45: ValueStore valueStore) throws IOException {
46: this .btreeIter = btreeIter;
47: this .valueStore = valueStore;
48:
49: this .nextValue = btreeIter.next();
50: }
51:
52: /*---------*
53: * Methods *
54: *---------*/
55:
56: public boolean hasNext() {
57: return nextValue != null;
58: }
59:
60: public Statement next() throws IOException {
61: if (nextValue == null) {
62: throw new NoSuchElementException();
63: }
64:
65: int subjID = ByteArrayUtil.getInt(nextValue,
66: TripleStore.SUBJ_IDX);
67: Resource subj = (Resource) valueStore.getValue(subjID);
68:
69: int predID = ByteArrayUtil.getInt(nextValue,
70: TripleStore.PRED_IDX);
71: URI pred = (URI) valueStore.getValue(predID);
72:
73: int objID = ByteArrayUtil
74: .getInt(nextValue, TripleStore.OBJ_IDX);
75: Value obj = valueStore.getValue(objID);
76:
77: Resource context = null;
78: int contextID = ByteArrayUtil.getInt(nextValue,
79: TripleStore.CONTEXT_IDX);
80: if (contextID != 0) {
81: context = (Resource) valueStore.getValue(contextID);
82: }
83:
84: nextValue = btreeIter.next();
85:
86: return valueStore.createStatement(subj, pred, obj, context);
87: }
88:
89: public void remove() {
90: throw new UnsupportedOperationException();
91: }
92:
93: @Override
94: protected void handleClose() throws IOException {
95: nextValue = null;
96: btreeIter.close();
97: }
98: }
|