@@ -54,26 +54,32 @@ impl Responses {
54
54
}
55
55
}
56
56
57
- struct State {
58
- /// A cached prepared statement for basic information for a type from its
57
+ /// A cache of type info and prepared statements for fetching type info
58
+ /// (corresponding to the queries in the [prepare](prepare) module).
59
+ #[ derive( Default ) ]
60
+ struct CachedTypeInfo {
61
+ /// A statement for basic information for a type from its
59
62
/// OID. Corresponds to [TYPEINFO_QUERY](prepare::TYPEINFO_QUERY) (or its
60
63
/// fallback).
61
64
typeinfo : Option < Statement > ,
62
- /// A cached prepared statement for getting information for a composite type
63
- /// from its OID. Corresponds to
64
- /// [TYPEINFO_QUERY](prepare::TYPEINFO_COMPOSITE_QUERY).
65
+ /// A statement for getting information for a composite type from its OID.
66
+ /// Corresponds to [TYPEINFO_QUERY](prepare::TYPEINFO_COMPOSITE_QUERY).
65
67
typeinfo_composite : Option < Statement > ,
66
- /// A cached prepared statement for getting information for a composite type
67
- /// from its OID. Corresponds to
68
- /// [TYPEINFO_QUERY](prepare::TYPEINFO_COMPOSITE_QUERY) (or its fallback).
68
+ /// A statement for getting information for a composite type from its OID.
69
+ /// Corresponds to [TYPEINFO_QUERY](prepare::TYPEINFO_COMPOSITE_QUERY) (or
70
+ /// its fallback).
69
71
typeinfo_enum : Option < Statement > ,
72
+
73
+ /// Cache of types already looked up.
70
74
types : HashMap < Oid , Type > ,
71
- buf : BytesMut ,
72
75
}
73
76
74
77
pub struct InnerClient {
75
78
sender : mpsc:: UnboundedSender < Request > ,
76
- state : Mutex < State > ,
79
+ cached_typeinfo : Mutex < CachedTypeInfo > ,
80
+
81
+ /// A buffer to use when writing out postgres commands.
82
+ buffer : Mutex < BytesMut > ,
77
83
}
78
84
79
85
impl InnerClient {
@@ -91,7 +97,7 @@ impl InnerClient {
91
97
}
92
98
93
99
pub fn typeinfo ( & self ) -> Option < Statement > {
94
- self . state . lock ( ) . typeinfo . clone ( )
100
+ self . cached_typeinfo . lock ( ) . typeinfo . clone ( )
95
101
}
96
102
97
103
pub fn set_typeinfo ( & self , statement : & Statement ) {
@@ -102,67 +108,61 @@ impl InnerClient {
102
108
// Note: We need to be sure that we don't drop a Statement while holding
103
109
// the state lock as its drop handling will call `with_buf`, which tries
104
110
// to take the lock.
105
- let mut state = self . state . lock ( ) ;
106
- if state . typeinfo . is_none ( ) {
107
- state . typeinfo = Some ( statement. clone ( ) ) ;
111
+ let mut cache = self . cached_typeinfo . lock ( ) ;
112
+ if cache . typeinfo . is_none ( ) {
113
+ cache . typeinfo = Some ( statement. clone ( ) ) ;
108
114
}
109
115
}
110
116
111
117
pub fn typeinfo_composite ( & self ) -> Option < Statement > {
112
- self . state . lock ( ) . typeinfo_composite . clone ( )
118
+ self . cached_typeinfo . lock ( ) . typeinfo_composite . clone ( )
113
119
}
114
120
115
121
pub fn set_typeinfo_composite ( & self , statement : & Statement ) {
116
122
// We only insert the statement if there isn't already a cached
117
123
// statement (this is safe as they are prepared statements for the same
118
124
// query).
119
- //
120
- // Note: We need to be sure that we don't drop a Statement while holding
121
- // the state lock as its drop handling will call `with_buf`, which tries
122
- // to take the lock.
123
- let mut state = self . state . lock ( ) ;
124
- if state. typeinfo_composite . is_none ( ) {
125
- state. typeinfo_composite = Some ( statement. clone ( ) ) ;
125
+ let mut cache = self . cached_typeinfo . lock ( ) ;
126
+ if cache. typeinfo_composite . is_none ( ) {
127
+ cache. typeinfo_composite = Some ( statement. clone ( ) ) ;
126
128
}
127
129
}
128
130
129
131
pub fn typeinfo_enum ( & self ) -> Option < Statement > {
130
- self . state . lock ( ) . typeinfo_enum . clone ( )
132
+ self . cached_typeinfo . lock ( ) . typeinfo_enum . clone ( )
131
133
}
132
134
133
135
pub fn set_typeinfo_enum ( & self , statement : & Statement ) {
134
136
// We only insert the statement if there isn't already a cached
135
137
// statement (this is safe as they are prepared statements for the same
136
138
// query).
137
- //
138
- // Note: We need to be sure that we don't drop a Statement while holding
139
- // the state lock as its drop handling will call `with_buf`, which tries
140
- // to take the lock.
141
- let mut state = self . state . lock ( ) ;
142
- if state. typeinfo_enum . is_none ( ) {
143
- state. typeinfo_enum = Some ( statement. clone ( ) ) ;
139
+ let mut cache = self . cached_typeinfo . lock ( ) ;
140
+ if cache. typeinfo_enum . is_none ( ) {
141
+ cache. typeinfo_enum = Some ( statement. clone ( ) ) ;
144
142
}
145
143
}
146
144
147
145
pub fn type_ ( & self , oid : Oid ) -> Option < Type > {
148
- self . state . lock ( ) . types . get ( & oid) . cloned ( )
146
+ self . cached_typeinfo . lock ( ) . types . get ( & oid) . cloned ( )
149
147
}
150
148
151
149
pub fn set_type ( & self , oid : Oid , type_ : & Type ) {
152
- self . state . lock ( ) . types . insert ( oid, type_. clone ( ) ) ;
150
+ self . cached_typeinfo . lock ( ) . types . insert ( oid, type_. clone ( ) ) ;
153
151
}
154
152
155
153
pub fn clear_type_cache ( & self ) {
156
- self . state . lock ( ) . types . clear ( ) ;
154
+ self . cached_typeinfo . lock ( ) . types . clear ( ) ;
157
155
}
158
156
157
+ /// Call the given function with a buffer to be used when writing out
158
+ /// postgres commands.
159
159
pub fn with_buf < F , R > ( & self , f : F ) -> R
160
160
where
161
161
F : FnOnce ( & mut BytesMut ) -> R ,
162
162
{
163
- let mut state = self . state . lock ( ) ;
164
- let r = f ( & mut state . buf ) ;
165
- state . buf . clear ( ) ;
163
+ let mut buffer = self . buffer . lock ( ) ;
164
+ let r = f ( & mut buffer ) ;
165
+ buffer . clear ( ) ;
166
166
r
167
167
}
168
168
}
@@ -199,13 +199,8 @@ impl Client {
199
199
Client {
200
200
inner : Arc :: new ( InnerClient {
201
201
sender,
202
- state : Mutex :: new ( State {
203
- typeinfo : None ,
204
- typeinfo_composite : None ,
205
- typeinfo_enum : None ,
206
- types : HashMap :: new ( ) ,
207
- buf : BytesMut :: new ( ) ,
208
- } ) ,
202
+ cached_typeinfo : Default :: default ( ) ,
203
+ buffer : Default :: default ( ) ,
209
204
} ) ,
210
205
#[ cfg( feature = "runtime" ) ]
211
206
socket_config : None ,
0 commit comments