@@ -49,53 +49,61 @@ public TriplePatternFragment getFragment(Resource subject, Property predicate, R
4949 throw new IllegalArgumentException ("limit" );
5050 }
5151
52- // look up the result from the HDT datasource
53- final int subjectId = subject == null ? 0 : dictionary .getIntID (subject .asNode (), TripleComponentRole .SUBJECT );
54- final int predicateId = predicate == null ? 0 : dictionary .getIntID (predicate .asNode (), TripleComponentRole .PREDICATE );
55- final int objectId = object == null ? 0 : dictionary .getIntID (object .asNode (), TripleComponentRole .OBJECT );
52+ // look up the result from the HDT datasource)
53+ int subjectId = subject == null ? 0 : dictionary .getIntID (subject .asNode (), TripleComponentRole .SUBJECT );
54+ int predicateId = predicate == null ? 0 : dictionary .getIntID (predicate .asNode (), TripleComponentRole .PREDICATE );
55+ int objectId = object == null ? 0 : dictionary .getIntID (object .asNode (), TripleComponentRole .OBJECT );
56+
5657 if (subjectId < 0 || predicateId < 0 || objectId < 0 ) {
5758 return new TriplePatternFragmentBase ();
5859 }
60+
5961 final Model triples = ModelFactory .createDefaultModel ();
60- final IteratorTripleID matches = datasource .getTriples ().search (new TripleID (subjectId , predicateId , objectId ));
61- final boolean hasMatches = matches .hasNext ();
62+ IteratorTripleID matches = datasource .getTriples ().search (new TripleID (subjectId , predicateId , objectId ));
63+ boolean hasMatches = matches .hasNext ();
6264
63- if (hasMatches ) {
64- // try to jump directly to the offset
65- boolean atOffset ;
66- if (matches .canGoTo ()) {
67- try {
68- matches .goTo (offset );
69- atOffset = true ;
70- }
71- // if the offset is outside the bounds, this page has no matches
72- catch (IndexOutOfBoundsException exception ) { atOffset = false ; }
73- }
74- // if not possible, advance to the offset iteratively
75- else {
76- matches .goToStart ();
77- for (int i = 0 ; !(atOffset = i == offset ) && matches .hasNext (); i ++)
78- matches .next ();
79- }
80- // try to add `limit` triples to the result model
81- if (atOffset ) {
82- for (int i = 0 ; i < limit && matches .hasNext (); i ++)
83- triples .add (triples .asStatement (toTriple (matches .next ())));
84- }
85- }
86-
87- // estimates can be wrong; ensure 0 is returned if there are no results, and always more than actual results
88- final long estimatedTotal = triples .size () > 0 ? Math .max (offset + triples .size () + 1 , matches .estimatedNumResults ())
89- : hasMatches ? Math .max (matches .estimatedNumResults (), 1 ) : 0 ;
65+ if (hasMatches ) {
66+ // try to jump directly to the offset
67+ boolean atOffset ;
68+ if (matches .canGoTo ()) {
69+ try {
70+ matches .goTo (offset );
71+ atOffset = true ;
72+ } // if the offset is outside the bounds, this page has no matches
73+ catch (IndexOutOfBoundsException exception ) {
74+ atOffset = false ;
75+ }
76+ } // if not possible, advance to the offset iteratively
77+ else {
78+ matches .goToStart ();
79+ for (int i = 0 ; !(atOffset = i == offset ) && matches .hasNext (); i ++) {
80+ matches .next ();
81+ }
82+ }
83+ // try to add `limit` triples to the result model
84+ if (atOffset ) {
85+ for (int i = 0 ; i < limit && matches .hasNext (); i ++) {
86+ triples .add (triples .asStatement (toTriple (matches .next ())));
87+ }
88+ }
89+ }
90+
91+ // estimates can be wrong; ensure 0 is returned if there are no results, and always more than actual results
92+ final long estimatedTotal = triples .size () > 0 ? Math .max (offset + triples .size () + 1 , matches .estimatedNumResults ())
93+ : hasMatches ? Math .max (matches .estimatedNumResults (), 1 ) : 0 ;
9094
9195 // create the fragment
9296 return new TriplePatternFragment () {
93- @ Override
94- public Model getTriples () { return triples ; }
95-
96- @ Override
97- public long getTotalSize () { return estimatedTotal ; }
98- };
97+ @ Override
98+ public Model getTriples () {
99+ return triples ;
100+ }
101+
102+ @ Override
103+ public long getTotalSize () {
104+ return estimatedTotal ;
105+ }
106+ };
99107 }
100108
101109 /**
@@ -106,9 +114,9 @@ public TriplePatternFragment getFragment(Resource subject, Property predicate, R
106114 */
107115 private Triple toTriple (TripleID tripleId ) {
108116 return new Triple (
109- dictionary .getNode (tripleId .getSubject (), TripleComponentRole .SUBJECT ),
110- dictionary .getNode (tripleId .getPredicate (), TripleComponentRole .PREDICATE ),
111- dictionary .getNode (tripleId .getObject (), TripleComponentRole .OBJECT )
117+ dictionary .getNode (tripleId .getSubject (), TripleComponentRole .SUBJECT ),
118+ dictionary .getNode (tripleId .getPredicate (), TripleComponentRole .PREDICATE ),
119+ dictionary .getNode (tripleId .getObject (), TripleComponentRole .OBJECT )
112120 );
113121 }
114122}
0 commit comments