-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSessionReference.java
More file actions
115 lines (97 loc) · 3.2 KB
/
Copy pathSessionReference.java
File metadata and controls
115 lines (97 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.cloud.spanner.SessionClient.SessionId;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import java.time.Instant;
import java.util.Map;
import javax.annotation.Nullable;
/**
* A {@code Session} can be used to perform transactions that read and/or modify data in a Cloud
* Spanner database. Sessions are managed internally by the client library, and users need not be
* aware of the actual session management, pooling and handling.
*/
class SessionReference {
private final String name;
private final DatabaseId databaseId;
@Nullable private final String databaseRole;
private final Map<SpannerRpc.Option, ?> options;
private volatile Instant lastUseTime;
@Nullable private final Instant createTime;
private final boolean isMultiplexed;
private volatile DatabaseMetadata databaseMetadata;
SessionReference(String name, @Nullable String databaseRole, Map<SpannerRpc.Option, ?> options) {
this.options = options;
this.name = checkNotNull(name);
this.databaseId = SessionId.of(name).getDatabaseId();
this.databaseRole = databaseRole;
this.lastUseTime = Instant.now();
this.createTime = null;
this.isMultiplexed = false;
}
SessionReference(
String name,
@Nullable String databaseRole,
com.google.protobuf.Timestamp createTime,
boolean isMultiplexed,
Map<SpannerRpc.Option, ?> options) {
this.options = options;
this.name = checkNotNull(name);
this.databaseId = SessionId.of(name).getDatabaseId();
this.databaseRole = databaseRole;
this.lastUseTime = Instant.now();
this.createTime = convert(createTime);
this.isMultiplexed = isMultiplexed;
}
public String getName() {
return name;
}
public String getDatabaseRole() {
return databaseRole;
}
public DatabaseId getDatabaseId() {
return databaseId;
}
Map<SpannerRpc.Option, ?> getOptions() {
return options;
}
Instant getLastUseTime() {
return lastUseTime;
}
Instant getCreateTime() {
return createTime;
}
boolean getIsMultiplexed() {
return isMultiplexed;
}
@Nullable
DatabaseMetadata getDatabaseMetadata() {
return databaseMetadata;
}
void setDatabaseMetadata(DatabaseMetadata databaseMetadata) {
this.databaseMetadata = databaseMetadata;
}
void markUsed(Instant instant) {
lastUseTime = instant;
}
private Instant convert(com.google.protobuf.Timestamp timestamp) {
if (timestamp == null) {
return null;
}
return Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
}
}