4

I want to achieve something like "optimistic locking" as mentioned in Optimistic vs. Pessimistic locking

I have the following data

:foo 
    :hasProp 'bar';
    :hasVersion '3'^^xsd:nonNegativeInteger
.

A sessions queries this data and keeps the information in memory. Then it wants to issue an update.

I only want an update to succeed, when the passed version is '3', which means that no update occurred since the session has read :foo.

Is there a way to make an update query that fails when the passed version is != 3 but otherwise succeeds and updates :hasVersion to '4'^^xsd:nonNegativeInteger and for example :hasProp to baz?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I think it's important to note and remember that SPARQL has no locking, nor transactions, nor various related features, unlike SQL. Generally speaking, each triple (or sometimes, each quad) is treated atomically. Future evolution will likely allow for each named graphs to be treated as an atomic (and potentially lockable) entity within a store, similar to tables in SQL. SQL's row-level-locking conceptually seems like a SPARQL lock on every triple that includes a given entity in `?s` or `?o` position. – TallTed Aug 29 '17 at 20:52
  • @TallTed thanks for the hint. I expected a single update query to be treated as a transaction. You think that's not the case. I'm currently experimenting with Stardog which supports transactions, but I'm interested in information about other systems as well. – Günter Zöchbauer Aug 30 '17 at 03:15
  • 2
    Each SPARQL query for SPARQL update should happen atomically when invoked by the SPARQL protocol. Local APIs may be different but probably offer some level of concurrency or transaction control. – AndyS Aug 30 '17 at 09:20
  • @AndyS thanks for confirming – Günter Zöchbauer Aug 30 '17 at 09:22

1 Answers1

3

"Fails" means simply that no data matches, i.e. you can use a FILTER. For the rest, just INSERT and DELETE the corresponding data:

PREFIX : <YOUR_NAMESPACE_HERE>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

INSERT {
  ?s :hasProp "baz" .
  ?s :hasVersion "4"^^xsd:nonNegativeInteger
}
DELETE {
  ?s :hasProp "bar" .
  ?s :hasVersion ?v
}
WHERE{
  ?s :hasVersion ?v
  FILTER(?v = "3"^^xsd:nonNegativeInteger)
}
RobV
  • 28,022
  • 11
  • 77
  • 119
UninformedUser
  • 8,397
  • 1
  • 14
  • 23
  • 2
    you want the filter to be `?v = 3` so as to avoid updating (to version 4) unless the current version is 3. as written, the query will update (to version 4) any other version except 3. – Jess Balint Aug 29 '17 at 20:50
  • Ehm, yes. You're right. I misread the question. Thank's. Also to @RobV – UninformedUser Aug 30 '17 at 14:07