|
| 1 | +import {Tabs} from 'nextra/components' |
| 2 | + |
| 3 | +# Versioning |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +Oxia will assign a version to all mutable operations, excluding those using put with`SequenceKeysDeltas.` |
| 8 | + |
| 9 | +Therefore, you can use this assigned version as the expected value for subsequent mutable operations to support `CompareThenSet-like` atomic operations. |
| 10 | + |
| 11 | +## Use cases |
| 12 | + |
| 13 | +The versioning is usually used for **Optimistic Concurrency Control (OCC)**. Instead of locking resources, each item has a version number. When you read an item, you get its version. When you try to update it, you send back the version you read. If the version on the server doesn't match, it means someone else updated the item in the interim, and your `put` fails. You then typically retry the operation by re-reading the latest version. |
| 14 | + |
| 15 | + |
| 16 | +## Using Versioning |
| 17 | + |
| 18 | +<Tabs items={['Go', 'Java']}> |
| 19 | + <Tabs.Tab> |
| 20 | + ```go |
| 21 | + // read version |
| 22 | + key, value, version, err := client.Get(context.Background(), "/my-key") |
| 23 | + if err != nil { |
| 24 | + log.Fatal(err) |
| 25 | + } |
| 26 | + // case-1: expect not exists |
| 27 | + _, res1, err := client.Put(context.Background(), "/my-key", []byte("value-1"), ExpectedRecordNotExists()) |
| 28 | + if err != nil { |
| 29 | + log.Fatal(err) |
| 30 | + } |
| 31 | + // case-2: expect by version |
| 32 | + _, _, err = client.Put(context.Background(), "/my-key", []byte("value-2"), ExpectedVersionId(version)) |
| 33 | + if err != nil { |
| 34 | + log.Fatal(err) |
| 35 | + } |
| 36 | + ``` |
| 37 | + </Tabs.Tab> |
| 38 | + <Tabs.Tab> |
| 39 | + ```java |
| 40 | + // read version |
| 41 | + var result = client.get("a"); |
| 42 | + |
| 43 | + // case-1: expect not exist |
| 44 | + var result = client.put("a", "a2".getBytes(UTF_8), Set.of(IfRecordDoesNotExist); |
| 45 | + |
| 46 | + // case-2: expect by version |
| 47 | + var result = client.put("a", "a2".getBytes(UTF_8), Set.of(IfVersionIdEquals(version))); |
| 48 | + ``` |
| 49 | + </Tabs.Tab> |
| 50 | +</Tabs> |
0 commit comments