Skip to content

Commit 2b9612d

Browse files
committed
Add dirty read support for document reads
1 parent 7133413 commit 2b9612d

File tree

8 files changed

+327
-188
lines changed

8 files changed

+327
-188
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
8181
// {"@value0": "users", "value1": "a", "value2": "b", "value3": "c"}
8282
```
8383

84+
- Added `allowDirtyRead` option to `db.query` and `collection.document`
85+
86+
Dirty reads are supported in leader/follower replication setups and require
87+
ArangoDB 3.4 or later. When performing a request that permits dirty reads,
88+
arangojs will load balance across all know leaders and followers and instruct
89+
ArangoDB to allow responding with stale or dirty response data. Note that
90+
data returned from a dirty read may be out of date or inconsistent.
91+
8492
## [6.6.0] - 2018-08-28
8593

8694
### Changed

docs/Drivers/JS/Reference/Collection/DocumentCollection.md

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The _DocumentCollection API_ extends the
55

66
## documentCollection.document
77

8-
`async documentCollection.document(documentHandle, [graceful]): Object`
8+
`async documentCollection.document(documentHandle, [opts]): Object`
99

1010
Retrieves the document with the given _documentHandle_ from the collection.
1111

@@ -17,22 +17,40 @@ Retrieves the document with the given _documentHandle_ from the collection.
1717
`_key` of a document in the collection, or a document (i.e. an object with an
1818
`_id` or `_key` property).
1919

20-
- **graceful**: `boolean` (Default: `false`)
20+
- **opts**: `Object` (optional)
21+
22+
If _opts_ is set, it must be an object with any of the following properties:
23+
24+
- **graceful**: `boolean` (Default: `false`)
25+
26+
If set to `true`, the method will return `null` instead of throwing an
27+
error if the document does not exist.
2128

22-
If set to `true`, the method will return `null` instead of throwing an error
23-
if the document does not exist.
29+
- **allowDirtyRead**: `boolean` (Default: `false`)
30+
31+
{% hint 'info' %}
32+
This option is only available when targeting ArangoDB 3.4 or later,
33+
see [Compatibility](../../GettingStarted/README.md#compatibility).
34+
{% endhint %}
35+
36+
If set to `true`, the request will explicitly permit ArangoDB to return a
37+
potentially dirty or stale result and arangojs will load balance the
38+
request without distinguishing between leaders and followers.
39+
40+
If a boolean is passed instead of an options object, it will be interpreted as
41+
the _graceful_ option.
2442

2543
**Examples**
2644

2745
```js
2846
const db = new Database();
29-
const collection = db.collection('my-docs');
47+
const collection = db.collection("my-docs");
3048

3149
try {
32-
const doc = await collection.document('some-key');
50+
const doc = await collection.document("some-key");
3351
// the document exists
34-
assert.equal(doc._key, 'some-key');
35-
assert.equal(doc._id, 'my-docs/some-key');
52+
assert.equal(doc._key, "some-key");
53+
assert.equal(doc._id, "my-docs/some-key");
3654
} catch (err) {
3755
// something went wrong or
3856
// the document does not exist
@@ -41,18 +59,18 @@ try {
4159
// -- or --
4260

4361
try {
44-
const doc = await collection.document('my-docs/some-key');
62+
const doc = await collection.document("my-docs/some-key");
4563
// the document exists
46-
assert.equal(doc._key, 'some-key');
47-
assert.equal(doc._id, 'my-docs/some-key');
64+
assert.equal(doc._key, "some-key");
65+
assert.equal(doc._id, "my-docs/some-key");
4866
} catch (err) {
4967
// something went wrong or
5068
// the document does not exist
5169
}
5270

5371
// -- or --
5472

55-
const doc = await collection.document('some-key', true);
73+
const doc = await collection.document("some-key", true);
5674
if (doc === null) {
5775
// the document does not exist
5876
}
@@ -76,9 +94,9 @@ Checks whether the document with the given _documentHandle_ exists.
7694

7795
```js
7896
const db = new Database();
79-
const collection = db.collection('my-docs');
97+
const collection = db.collection("my-docs");
8098

81-
const exists = await collection.documentExists('some-key');
99+
const exists = await collection.documentExists("some-key");
82100
if (exists === false) {
83101
// the document does not exist
84102
}
@@ -137,22 +155,22 @@ For more information on the _opts_ object, see the
137155

138156
```js
139157
const db = new Database();
140-
const collection = db.collection('my-docs');
141-
const data = {some: 'data'};
158+
const collection = db.collection("my-docs");
159+
const data = { some: "data" };
142160
const info = await collection.save(data);
143-
assert.equal(info._id, 'my-docs/' + info._key);
144-
const doc2 = await collection.document(info)
161+
assert.equal(info._id, "my-docs/" + info._key);
162+
const doc2 = await collection.document(info);
145163
assert.equal(doc2._id, info._id);
146164
assert.equal(doc2._rev, info._rev);
147165
assert.equal(doc2.some, data.some);
148166

149167
// -- or --
150168

151169
const db = new Database();
152-
const collection = db.collection('my-docs');
153-
const data = {some: 'data'};
154-
const opts = {returnNew: true};
155-
const doc = await collection.save(data, opts)
156-
assert.equal(doc1._id, 'my-docs/' + doc1._key);
170+
const collection = db.collection("my-docs");
171+
const data = { some: "data" };
172+
const opts = { returnNew: true };
173+
const doc = await collection.save(data, opts);
174+
assert.equal(doc1._id, "my-docs/" + doc1._key);
157175
assert.equal(doc1.new.some, data.some);
158176
```

docs/Drivers/JS/Reference/Collection/EdgeCollection.md

Lines changed: 75 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The _EdgeCollection API_ extends the
55

66
## edgeCollection.document
77

8-
`async edgeCollection.document(documentHandle, [graceful]): Object`
8+
`async edgeCollection.document(documentHandle, [opts]): Object`
99

1010
Alias: `edgeCollection.edge`.
1111

@@ -19,32 +19,50 @@ Retrieves the edge with the given _documentHandle_ from the collection.
1919
of an edge in the collection, or an edge (i.e. an object with an `_id` or
2020
`_key` property).
2121

22-
* **graceful**: `boolean` (Default: `false`)
22+
- **opts**: `Object` (optional)
23+
24+
If _opts_ is set, it must be an object with any of the following properties:
25+
26+
- **graceful**: `boolean` (Default: `false`)
27+
28+
If set to `true`, the method will return `null` instead of throwing an
29+
error if the edge does not exist.
2330

24-
If set to `true`, the method will return `null` instead of throwing an error
25-
if the edge does not exist.
31+
- **allowDirtyRead**: `boolean` (Default: `false`)
32+
33+
{% hint 'info' %}
34+
This option is only available when targeting ArangoDB 3.4 or later,
35+
see [Compatibility](../../GettingStarted/README.md#compatibility).
36+
{% endhint %}
37+
38+
If set to `true`, the request will explicitly permit ArangoDB to return a
39+
potentially dirty or stale result and arangojs will load balance the
40+
request without distinguishing between leaders and followers.
41+
42+
If a boolean is passed instead of an options object, it will be interpreted as
43+
the _graceful_ option.
2644

2745
**Examples**
2846

2947
```js
3048
const db = new Database();
31-
const collection = db.edgeCollection('edges');
49+
const collection = db.edgeCollection("edges");
3250

33-
const edge = await collection.document('some-key');
51+
const edge = await collection.document("some-key");
3452
// the edge exists
35-
assert.equal(edge._key, 'some-key');
36-
assert.equal(edge._id, 'edges/some-key');
53+
assert.equal(edge._key, "some-key");
54+
assert.equal(edge._id, "edges/some-key");
3755

3856
// -- or --
3957

40-
const edge = await collection.document('edges/some-key');
58+
const edge = await collection.document("edges/some-key");
4159
// the edge exists
42-
assert.equal(edge._key, 'some-key');
43-
assert.equal(edge._id, 'edges/some-key');
60+
assert.equal(edge._key, "some-key");
61+
assert.equal(edge._id, "edges/some-key");
4462

4563
// -- or --
4664

47-
const edge = await collection.document('some-key', true);
65+
const edge = await collection.document("some-key", true);
4866
if (edge === null) {
4967
// the edge does not exist
5068
}
@@ -68,9 +86,9 @@ Checks whether the edge with the given _documentHandle_ exists.
6886

6987
```js
7088
const db = new Database();
71-
const collection = db.edgeCollection('my-docs');
89+
const collection = db.edgeCollection("my-docs");
7290

73-
const exists = await collection.documentExists('some-key');
91+
const exists = await collection.documentExists("some-key");
7492
if (exists === false) {
7593
// the edge does not exist
7694
}
@@ -139,28 +157,28 @@ the _returnNew_ option.
139157

140158
```js
141159
const db = new Database();
142-
const collection = db.edgeCollection('edges');
143-
const data = {some: 'data'};
160+
const collection = db.edgeCollection("edges");
161+
const data = { some: "data" };
144162

145163
const info = await collection.save(
146164
data,
147-
'vertices/start-vertex',
148-
'vertices/end-vertex'
165+
"vertices/start-vertex",
166+
"vertices/end-vertex"
149167
);
150-
assert.equal(info._id, 'edges/' + info._key);
151-
const edge = await collection.edge(edge)
168+
assert.equal(info._id, "edges/" + info._key);
169+
const edge = await collection.edge(edge);
152170
assert.equal(edge._key, info._key);
153171
assert.equal(edge._rev, info._rev);
154172
assert.equal(edge.some, data.some);
155-
assert.equal(edge._from, 'vertices/start-vertex');
156-
assert.equal(edge._to, 'vertices/end-vertex');
173+
assert.equal(edge._from, "vertices/start-vertex");
174+
assert.equal(edge._to, "vertices/end-vertex");
157175

158176
// -- or --
159177

160178
const info = await collection.save({
161-
some: 'data',
162-
_from: 'verticies/start-vertex',
163-
_to: 'vertices/end-vertex'
179+
some: "data",
180+
_from: "verticies/start-vertex",
181+
_to: "vertices/end-vertex"
164182
});
165183
// ...
166184
```
@@ -183,16 +201,16 @@ Retrieves a list of all edges of the document with the given _documentHandle_.
183201

184202
```js
185203
const db = new Database();
186-
const collection = db.edgeCollection('edges');
204+
const collection = db.edgeCollection("edges");
187205
await collection.import([
188-
['_key', '_from', '_to'],
189-
['x', 'vertices/a', 'vertices/b'],
190-
['y', 'vertices/a', 'vertices/c'],
191-
['z', 'vertices/d', 'vertices/a']
192-
])
193-
const edges = await collection.edges('vertices/a');
206+
["_key", "_from", "_to"],
207+
["x", "vertices/a", "vertices/b"],
208+
["y", "vertices/a", "vertices/c"],
209+
["z", "vertices/d", "vertices/a"]
210+
]);
211+
const edges = await collection.edges("vertices/a");
194212
assert.equal(edges.length, 3);
195-
assert.deepEqual(edges.map(edge => edge._key), ['x', 'y', 'z']);
213+
assert.deepEqual(edges.map(edge => edge._key), ["x", "y", "z"]);
196214
```
197215

198216
## edgeCollection.inEdges
@@ -214,16 +232,16 @@ _documentHandle_.
214232

215233
```js
216234
const db = new Database();
217-
const collection = db.edgeCollection('edges');
235+
const collection = db.edgeCollection("edges");
218236
await collection.import([
219-
['_key', '_from', '_to'],
220-
['x', 'vertices/a', 'vertices/b'],
221-
['y', 'vertices/a', 'vertices/c'],
222-
['z', 'vertices/d', 'vertices/a']
237+
["_key", "_from", "_to"],
238+
["x", "vertices/a", "vertices/b"],
239+
["y", "vertices/a", "vertices/c"],
240+
["z", "vertices/d", "vertices/a"]
223241
]);
224-
const edges = await collection.inEdges('vertices/a');
242+
const edges = await collection.inEdges("vertices/a");
225243
assert.equal(edges.length, 1);
226-
assert.equal(edges[0]._key, 'z');
244+
assert.equal(edges[0]._key, "z");
227245
```
228246

229247
## edgeCollection.outEdges
@@ -245,16 +263,16 @@ _documentHandle_.
245263

246264
```js
247265
const db = new Database();
248-
const collection = db.edgeCollection('edges');
266+
const collection = db.edgeCollection("edges");
249267
await collection.import([
250-
['_key', '_from', '_to'],
251-
['x', 'vertices/a', 'vertices/b'],
252-
['y', 'vertices/a', 'vertices/c'],
253-
['z', 'vertices/d', 'vertices/a']
268+
["_key", "_from", "_to"],
269+
["x", "vertices/a", "vertices/b"],
270+
["y", "vertices/a", "vertices/c"],
271+
["z", "vertices/d", "vertices/a"]
254272
]);
255-
const edges = await collection.outEdges('vertices/a');
273+
const edges = await collection.outEdges("vertices/a");
256274
assert.equal(edges.length, 2);
257-
assert.deepEqual(edges.map(edge => edge._key), ['x', 'y']);
275+
assert.deepEqual(edges.map(edge => edge._key), ["x", "y"]);
258276
```
259277

260278
## edgeCollection.traversal
@@ -288,17 +306,17 @@ contained in this edge collection.
288306

289307
```js
290308
const db = new Database();
291-
const collection = db.edgeCollection('edges');
309+
const collection = db.edgeCollection("edges");
292310
await collection.import([
293-
['_key', '_from', '_to'],
294-
['x', 'vertices/a', 'vertices/b'],
295-
['y', 'vertices/b', 'vertices/c'],
296-
['z', 'vertices/c', 'vertices/d']
311+
["_key", "_from", "_to"],
312+
["x", "vertices/a", "vertices/b"],
313+
["y", "vertices/b", "vertices/c"],
314+
["z", "vertices/c", "vertices/d"]
297315
]);
298-
const result = await collection.traversal('vertices/a', {
299-
direction: 'outbound',
300-
visitor: 'result.vertices.push(vertex._key);',
301-
init: 'result.vertices = [];'
316+
const result = await collection.traversal("vertices/a", {
317+
direction: "outbound",
318+
visitor: "result.vertices.push(vertex._key);",
319+
init: "result.vertices = [];"
302320
});
303-
assert.deepEqual(result.vertices, ['a', 'b', 'c', 'd']);
321+
assert.deepEqual(result.vertices, ["a", "b", "c", "d"]);
304322
```

0 commit comments

Comments
 (0)