@@ -54,17 +54,32 @@ impl Responses {
54
54
}
55
55
}
56
56
57
- struct State {
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
62
+ /// OID. Corresponds to [TYPEINFO_QUERY](prepare::TYPEINFO_QUERY) (or its
63
+ /// fallback).
58
64
typeinfo : Option < Statement > ,
65
+ /// A statement for getting information for a composite type from its OID.
66
+ /// Corresponds to [TYPEINFO_QUERY](prepare::TYPEINFO_COMPOSITE_QUERY).
59
67
typeinfo_composite : Option < Statement > ,
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).
60
71
typeinfo_enum : Option < Statement > ,
72
+
73
+ /// Cache of types already looked up.
61
74
types : HashMap < Oid , Type > ,
62
- buf : BytesMut ,
63
75
}
64
76
65
77
pub struct InnerClient {
66
78
sender : mpsc:: UnboundedSender < Request > ,
67
- state : Mutex < State > ,
79
+ cached_typeinfo : Mutex < CachedTypeInfo > ,
80
+
81
+ /// A buffer to use when writing out postgres commands.
82
+ buffer : Mutex < BytesMut > ,
68
83
}
69
84
70
85
impl InnerClient {
@@ -82,48 +97,50 @@ impl InnerClient {
82
97
}
83
98
84
99
pub fn typeinfo ( & self ) -> Option < Statement > {
85
- self . state . lock ( ) . typeinfo . clone ( )
100
+ self . cached_typeinfo . lock ( ) . typeinfo . clone ( )
86
101
}
87
102
88
103
pub fn set_typeinfo ( & self , statement : & Statement ) {
89
- self . state . lock ( ) . typeinfo = Some ( statement. clone ( ) ) ;
104
+ self . cached_typeinfo . lock ( ) . typeinfo = Some ( statement. clone ( ) ) ;
90
105
}
91
106
92
107
pub fn typeinfo_composite ( & self ) -> Option < Statement > {
93
- self . state . lock ( ) . typeinfo_composite . clone ( )
108
+ self . cached_typeinfo . lock ( ) . typeinfo_composite . clone ( )
94
109
}
95
110
96
111
pub fn set_typeinfo_composite ( & self , statement : & Statement ) {
97
- self . state . lock ( ) . typeinfo_composite = Some ( statement. clone ( ) ) ;
112
+ self . cached_typeinfo . lock ( ) . typeinfo_composite = Some ( statement. clone ( ) ) ;
98
113
}
99
114
100
115
pub fn typeinfo_enum ( & self ) -> Option < Statement > {
101
- self . state . lock ( ) . typeinfo_enum . clone ( )
116
+ self . cached_typeinfo . lock ( ) . typeinfo_enum . clone ( )
102
117
}
103
118
104
119
pub fn set_typeinfo_enum ( & self , statement : & Statement ) {
105
- self . state . lock ( ) . typeinfo_enum = Some ( statement. clone ( ) ) ;
120
+ self . cached_typeinfo . lock ( ) . typeinfo_enum = Some ( statement. clone ( ) ) ;
106
121
}
107
122
108
123
pub fn type_ ( & self , oid : Oid ) -> Option < Type > {
109
- self . state . lock ( ) . types . get ( & oid) . cloned ( )
124
+ self . cached_typeinfo . lock ( ) . types . get ( & oid) . cloned ( )
110
125
}
111
126
112
127
pub fn set_type ( & self , oid : Oid , type_ : & Type ) {
113
- self . state . lock ( ) . types . insert ( oid, type_. clone ( ) ) ;
128
+ self . cached_typeinfo . lock ( ) . types . insert ( oid, type_. clone ( ) ) ;
114
129
}
115
130
116
131
pub fn clear_type_cache ( & self ) {
117
- self . state . lock ( ) . types . clear ( ) ;
132
+ self . cached_typeinfo . lock ( ) . types . clear ( ) ;
118
133
}
119
134
135
+ /// Call the given function with a buffer to be used when writing out
136
+ /// postgres commands.
120
137
pub fn with_buf < F , R > ( & self , f : F ) -> R
121
138
where
122
139
F : FnOnce ( & mut BytesMut ) -> R ,
123
140
{
124
- let mut state = self . state . lock ( ) ;
125
- let r = f ( & mut state . buf ) ;
126
- state . buf . clear ( ) ;
141
+ let mut buffer = self . buffer . lock ( ) ;
142
+ let r = f ( & mut buffer ) ;
143
+ buffer . clear ( ) ;
127
144
r
128
145
}
129
146
}
@@ -160,13 +177,8 @@ impl Client {
160
177
Client {
161
178
inner : Arc :: new ( InnerClient {
162
179
sender,
163
- state : Mutex :: new ( State {
164
- typeinfo : None ,
165
- typeinfo_composite : None ,
166
- typeinfo_enum : None ,
167
- types : HashMap :: new ( ) ,
168
- buf : BytesMut :: new ( ) ,
169
- } ) ,
180
+ cached_typeinfo : Default :: default ( ) ,
181
+ buffer : Default :: default ( ) ,
170
182
} ) ,
171
183
#[ cfg( feature = "runtime" ) ]
172
184
socket_config : None ,
0 commit comments