3333import org .eclipse .rdf4j .model .vocabulary .XSD ;
3434import org .eclipse .rdf4j .sail .SailException ;
3535import org .eclipse .rdf4j .sail .nativerdf .datastore .DataStore ;
36+ import org .eclipse .rdf4j .sail .nativerdf .model .CorruptValue ;
3637import org .eclipse .rdf4j .sail .nativerdf .model .NativeBNode ;
3738import org .eclipse .rdf4j .sail .nativerdf .model .NativeIRI ;
3839import org .eclipse .rdf4j .sail .nativerdf .model .NativeLiteral ;
@@ -123,6 +124,11 @@ public class ValueStore extends SimpleValueFactory {
123124 */
124125 private final ConcurrentCache <String , Integer > namespaceIDCache ;
125126
127+ /**
128+ * Do not throw an exception in case a value cannot be loaded, e.g. due to a corrupt value store.
129+ */
130+ private final boolean softFailOnCorruptData ;
131+
126132 /*--------------*
127133 * Constructors *
128134 *--------------*/
@@ -146,6 +152,15 @@ public ValueStore(File dataDir, boolean forceSync, int valueCacheSize, int value
146152 namespaceIDCache = new ConcurrentCache <>(namespaceIDCacheSize );
147153
148154 setNewRevision ();
155+
156+ /*
157+ * Soft failure when a ValueStore is corrupt (i.e., one or more NativeValues cannot be read properly) can be
158+ * enabled using the system property org.eclipse.rdf4j.sail.nativerdf.softFailOnCorruptData (boolean). The
159+ * default behavior is that ValueStore will fail hard with a SailException, whereas softFaileOnCorruptData set
160+ * to true will make ValueStore return instances of CorruptValue if NativeValue cannot be read.
161+ */
162+ this .softFailOnCorruptData = "true"
163+ .equalsIgnoreCase (System .getProperty ("org.eclipse.rdf4j.sail.nativerdf.softFailOnCorruptData" ));
149164 }
150165
151166 /*---------*
@@ -526,6 +541,12 @@ private boolean isNamespaceData(byte[] data) {
526541 }
527542
528543 private NativeValue data2value (int id , byte [] data ) throws IOException {
544+ if (data .length == 0 ) {
545+ if (softFailOnCorruptData ) {
546+ return new CorruptValue (revision , id );
547+ }
548+ throw new SailException ("Empty data array for value with id " + id );
549+ }
529550 switch (data [0 ]) {
530551 case URI_VALUE :
531552 return data2uri (id , data );
@@ -534,7 +555,10 @@ private NativeValue data2value(int id, byte[] data) throws IOException {
534555 case LITERAL_VALUE :
535556 return data2literal (id , data );
536557 default :
537- throw new IllegalArgumentException ("Invalid type " + data [0 ] + " for value with id " + id );
558+ if (softFailOnCorruptData ) {
559+ return new CorruptValue (revision , id );
560+ }
561+ throw new SailException ("Invalid type " + data [0 ] + " for value with id " + id );
538562 }
539563 }
540564
0 commit comments