@Override public void delete(final URI type, final URI id) throws IOException, IllegalStateException { // Obtain the statements to delete throuh a lookup final List<Statement> statements = Record.encode( lookup(type, ImmutableSet.of(id), null), ImmutableList.of(type)).toList(); // Perform the deletion this.transaction.remove(statements); }
private synchronized void update(final Map<URI, Map<URI, Record>> tables, final int revision) throws IOException { if (this.revision != revision) { throw new IOException("Commit failed due to concurrent modifications " + this.revision + ", " + revision); } OutputStream stream = null; try { stream = Files.writeWithBackup(this.fileSystem, this.filePath); final List<Record> records = Lists.newArrayList(); for (final URI type : tables.keySet()) { records.addAll(tables.get(type).values()); } final RDFFormat format = RDFFormat.forFileName(this.filePath.getName()); RDFUtil.writeRDF(stream, format, Data.getNamespaceMap(), null, Record.encode(Stream.create(records), ImmutableSet.<URI>of())); ++this.revision; this.tables = tables; MemoryDataStore.LOGGER.info("MemoryDataStore updated, {} records persisted", records.size()); } catch (final Throwable ex) { MemoryDataStore.LOGGER.error("MemoryDataStore update failed", ex); } finally { Util.closeQuietly(stream); } }
private static void write(final Stream<Record> axioms, final OutputStream stream, @Nullable final String formatString) throws IOException { // Determine target RDF format and compression based on format string final Compression compression = detectCompression(formatString, Compression.NONE); final RDFFormat format = detectRDFFormat(formatString, null); if (format == null) { if (formatString == null) { throw new IllegalArgumentException( "Must specify output format (-t) if writing to STDOUT"); } else { throw new IllegalArgumentException("Cannot detect RDF format for " + formatString); } } // Setup compression, if necessary final OutputStream actualStream = compression.write(Data.getExecutor(), stream); // Performs writing RDFUtil.writeRDF(actualStream, format, Data.getNamespaceMap(), null, Record.encode(axioms, ImmutableSet.of(KS.AXIOM))); }
@Override public void handle(final Record record) throws Throwable { if (record == null || this.records > 0 && this.records % 1000 == 0) { LOGGER.info(this.records + " records, " + this.triples + " triples processed"); } if (record != null) { final List<Statement> statements = Record.encode(Stream.create(record), ImmutableSet.of()).toList(); writer.handleComment(record.getID().toString()); for (final Statement statement : statements) { writer.handleStatement(statement); } ++this.records; this.triples += statements.size(); } }
@Override public void store(final URI type, final Record record) throws IOException, IllegalStateException { // Delete existing data for the record URI delete(type, record.getID()); // Add statements final List<Statement> statements = Record.encode(Stream.create(record), ImmutableList.of(type)).toList(); this.transaction.add(statements); }
private static void write(final Stream<Record> axioms, final File file, @Nullable final String formatString) throws IOException { // Determine target RDF format and compression based on format string Compression compression = detectCompression(file.getName(), null); if (compression == null) { compression = detectCompression(formatString, Compression.NONE); } RDFFormat format = detectRDFFormat(file.getName(), null); if (format == null) { format = detectRDFFormat(formatString, null); } if (format == null) { throw new IllegalArgumentException("Cannot detect RDF format of " + file); } // Setup compression, if necessary final OutputStream actualStream = compression.write(Data.getExecutor(), file); // Performs writing try { RDFUtil.writeRDF(actualStream, format, Data.getNamespaceMap(), null, Record.encode(axioms, ImmutableSet.of(KS.AXIOM))); } finally { Util.closeQuietly(actualStream); } }
@Override void doEvaluate(final Session session, final BindingSet input, final MapBindingSet output) throws Throwable { final URI id = (URI) Statements.parseValue(this.id.instantiate(input), Namespaces.DEFAULT); long numTriples = 0L; try { numTriples += Record.encode(session.retrieve(KS.RESOURCE).ids(id).exec(), ImmutableList.of(KS.RESOURCE)).count(); numTriples += Record.encode( session.retrieve(KS.MENTION).condition("ks:mentionOf = $$", id) .limit(100000L).exec(), ImmutableList.of(KS.MENTION)).count(); if (numTriples == 0) { LOGGER.warn("No results for LOOKUP ALL request, id " + id); } } catch (final Throwable ex) { throw new RuntimeException("Failed LOOKUP ALL, id " + TestUtil.format(id), ex); } finally { output.addBinding("size", FACTORY.createLiteral(numTriples)); } } }
@Override void doEvaluate(final Session session, final BindingSet input, final MapBindingSet output) throws Throwable { final String condition = Strings.nullToEmpty(this.condition.instantiate(input)); long numTriples = 0L; try { // FIXME: conditions do not seem to work final Stream<Record> stream = session.retrieve(this.layer) .condition(condition).offset(this.offset).limit(this.limit) .properties(this.properties).exec(); numTriples = Record.encode(stream, ImmutableList.of(this.layer)).count(); if (numTriples == 0) { LOGGER.warn("No results for RETRIEVE request, layer " + TestUtil.format(this.layer) + ", condition '" + condition + "', offset " + this.offset + ", limit " + this.limit); } } catch (final Throwable ex) { throw new RuntimeException("Failed RETRIEVE " + TestUtil.format(this.layer) + ", condition " + condition + ", offset " + this.offset + ", limit" + this.limit + ", properties " + this.properties, ex); } finally { output.addBinding("size", FACTORY.createLiteral(numTriples)); } } }
@Override void doEvaluate(final Session session, final BindingSet input, final MapBindingSet output) throws Throwable { final URI id = (URI) Statements.parseValue(this.id.instantiate(input), Namespaces.DEFAULT); long numTriples = 0L; try { final Stream<Record> stream = session.retrieve(this.layer).ids(id) .properties(this.properties).exec(); numTriples = Record.encode(stream, ImmutableList.of(this.layer)).count(); if (numTriples == 0) { LOGGER.warn("No results for LOOKUP request, layer " + TestUtil.format(this.layer) + ", id " + id); } } catch (final Throwable ex) { throw new RuntimeException("Failed LOOKUP " + TestUtil.format(this.layer) + ", id " + TestUtil.format(id) + ", properties " + this.properties, ex); } finally { output.addBinding("size", FACTORY.createLiteral(numTriples)); } }