1/*
2 * snapshot.go
3 *
4 * This source file is part of the FoundationDB open source project
5 *
6 * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *     http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21// FoundationDB Go API
22
23package fdb
24
25// Snapshot is a handle to a FoundationDB transaction snapshot, suitable for
26// performing snapshot reads. Snapshot reads offer a more relaxed isolation
27// level than FoundationDB's default serializable isolation, reducing
28// transaction conflicts but making it harder to reason about concurrency.
29//
30// For more information on snapshot reads, see
31// https://apple.github.io/foundationdb/developer-guide.html#snapshot-reads.
32type Snapshot struct {
33	*transaction
34}
35
36// ReadTransact executes the caller-provided function, passing it the Snapshot
37// receiver object (as a ReadTransaction).
38//
39// A panic of type Error during execution of the function will be recovered and
40// returned to the caller as an error, but ReadTransact will not retry the
41// function.
42//
43// By satisfying the ReadTransactor interface, Snapshot may be passed to a
44// read-only transactional function from another (possibly read-only)
45// transactional function, allowing composition.
46//
47// See the ReadTransactor interface for an example of using ReadTransact with
48// Transaction, Snapshot and Database objects.
49func (s Snapshot) ReadTransact(f func(ReadTransaction) (interface{}, error)) (r interface{}, e error) {
50	defer panicToError(&e)
51
52	r, e = f(s)
53	return
54}
55
56// Snapshot returns the receiver and allows Snapshot to satisfy the
57// ReadTransaction interface.
58func (s Snapshot) Snapshot() Snapshot {
59	return s
60}
61
62// Get is equivalent to (Transaction).Get, performed as a snapshot read.
63func (s Snapshot) Get(key KeyConvertible) FutureByteSlice {
64	return s.get(key.FDBKey(), 1)
65}
66
67// GetKey is equivalent to (Transaction).GetKey, performed as a snapshot read.
68func (s Snapshot) GetKey(sel Selectable) FutureKey {
69	return s.getKey(sel.FDBKeySelector(), 1)
70}
71
72// GetRange is equivalent to (Transaction).GetRange, performed as a snapshot
73// read.
74func (s Snapshot) GetRange(r Range, options RangeOptions) RangeResult {
75	return s.getRange(r, options, true)
76}
77
78// GetReadVersion is equivalent to (Transaction).GetReadVersion, performed as
79// a snapshot read.
80func (s Snapshot) GetReadVersion() FutureInt64 {
81	return s.getReadVersion()
82}
83
84// GetDatabase returns a handle to the database with which this snapshot is
85// interacting.
86func (s Snapshot) GetDatabase() Database {
87	return s.transaction.db
88}
89