@@ -36,9 +36,9 @@ open Db_ref
36
36
37
37
let fist_delay_read_records_where = ref false
38
38
39
- type field_in = string
39
+ type field_in = Schema.Value .t
40
40
41
- type field_out = string
41
+ type field_out = Schema .maybe_cached_value
42
42
43
43
(* Only needed by the DB_ACCESS signature *)
44
44
let initialise () = ()
@@ -53,14 +53,13 @@ let is_valid_ref t objref =
53
53
54
54
let read_field_internal _ tblname fldname objref db =
55
55
try
56
- Row. find fldname
56
+ Row. find' fldname
57
57
(Table. find objref (TableSet. find tblname (Database. tableset db)))
58
58
with Not_found -> raise (DBCache_NotFound (" missing row" , tblname, objref))
59
59
60
60
(* Read field from cache *)
61
61
let read_field t tblname fldname objref =
62
- Schema.Value. marshal
63
- (read_field_internal t tblname fldname objref (get_database t))
62
+ read_field_internal t tblname fldname objref (get_database t)
64
63
65
64
(* * Finds the longest XML-compatible UTF-8 prefix of the given
66
65
string, by truncating the string at the first incompatible
@@ -75,6 +74,8 @@ let ensure_utf8_xml string =
75
74
warn " string truncated to: '%s'." prefix ;
76
75
prefix
77
76
77
+ let ensure_utf8_xml_and_share string = string |> ensure_utf8_xml |> Share. merge
78
+
78
79
(* Write field in cache *)
79
80
let write_field_locked t tblname objref fldname newval =
80
81
let current_val = get_field tblname objref fldname (get_database t) in
@@ -86,10 +87,6 @@ let write_field_locked t tblname objref fldname newval =
86
87
)
87
88
88
89
let write_field t tblname objref fldname newval =
89
- let schema = Database. schema (get_database t) in
90
- let schema = Schema. table tblname schema in
91
- let column = Schema.Table. find fldname schema in
92
- let newval = Schema.Value. unmarshal column.Schema.Column. ty newval in
93
90
let newval =
94
91
match newval with
95
92
| Schema.Value. String s ->
@@ -114,7 +111,7 @@ let touch_row t tblname objref =
114
111
and iterates through set-refs [returning (fieldname, ref list) list; where fieldname is the
115
112
name of the Set Ref field in tbl; and ref list is the list of foreign keys from related
116
113
table with remote-fieldname=objref] *)
117
- let read_record_internal db tblname objref =
114
+ let read_record_internal conv db tblname objref =
118
115
try
119
116
let tbl = TableSet. find tblname (Database. tableset db) in
120
117
let row = Table. find objref tbl in
@@ -138,15 +135,14 @@ let read_record_internal db tblname objref =
138
135
| None ->
139
136
accum_setref
140
137
in
141
- let accum_fvlist =
142
- (k, Schema.CachedValue. string_of cached) :: accum_fvlist
143
- in
138
+ let accum_fvlist = (k, conv cached) :: accum_fvlist in
144
139
(accum_fvlist, accum_setref)
145
140
)
146
141
row ([] , [] )
147
142
with Not_found -> raise (DBCache_NotFound (" missing row" , tblname, objref))
148
143
149
- let read_record t = read_record_internal (get_database t)
144
+ let read_record t =
145
+ read_record_internal Schema.CachedValue. open_present (get_database t)
150
146
151
147
(* Delete row from tbl *)
152
148
let delete_row_locked t tblname objref =
@@ -177,7 +173,7 @@ let create_row_locked t tblname kvs' new_objref =
177
173
let db = get_database t in
178
174
let g = Manifest. generation (Database. manifest db) in
179
175
let row =
180
- List. fold_left (fun row (k , v ) -> Row. add g k v row) Row. empty kvs'
176
+ List. fold_left (fun row (k , v ) -> Row. add' g k v row) Row. empty kvs'
181
177
in
182
178
let schema = Schema. table tblname (Database. schema db) in
183
179
(* fill in default values if kv pairs for these are not supplied already *)
@@ -195,31 +191,13 @@ let create_row_locked t tblname kvs' new_objref =
195
191
(get_database t)
196
192
197
193
let fld_check t tblname objref (fldname , value ) =
198
- let v = read_field_internal t tblname fldname objref (get_database t) in
199
- (v = value, fldname, v)
200
-
201
- let create_row t tblname kvs' new_objref =
202
- let schema = Database. schema (get_database t) in
203
- let schema = Schema. table tblname schema in
204
- let kvs' =
205
- List. map
206
- (fun (key , value ) ->
207
- let value = ensure_utf8_xml value in
208
- let column = Schema.Table. find key schema in
209
- let newval =
210
- match Schema.Value. unmarshal column.Schema.Column. ty value with
211
- | Schema.Value. String x ->
212
- Schema.Value. String (Share. merge x)
213
- | Schema.Value. Pairs ps ->
214
- Schema.Value. Pairs
215
- (List. map (fun (x , y ) -> (Share. merge x, Share. merge y)) ps)
216
- | Schema.Value. Set xs ->
217
- Schema.Value. Set (List. map Share. merge xs)
218
- in
219
- (key, newval)
220
- )
221
- kvs'
194
+ let v =
195
+ Schema.CachedValue. string_of
196
+ (read_field_internal t tblname fldname objref (get_database t))
222
197
in
198
+ (v = Schema.CachedValue. string_of value, fldname, v)
199
+
200
+ let create_row' t tblname kvs' new_objref =
223
201
with_lock (fun () ->
224
202
if is_valid_ref t new_objref then
225
203
let uniq_check_list = List. map (fld_check t tblname new_objref) kvs' in
@@ -228,37 +206,69 @@ let create_row t tblname kvs' new_objref =
228
206
in
229
207
match failure_opt with
230
208
| Some (_ , f , v ) ->
231
- raise (Integrity_violation (tblname, f, Schema.Value. marshal v))
209
+ raise (Integrity_violation (tblname, f, v))
232
210
| _ ->
233
211
()
234
212
else
235
213
(* we add the reference to the row itself so callers can use read_field_where to
236
214
return the reference: awkward if it is just the key *)
237
- let kvs' = (Db_names. ref , Schema.Value. string new_objref) :: kvs' in
215
+ let kvs' =
216
+ (Db_names. ref , Schema.Value. string new_objref |> Schema.CachedValue. v)
217
+ :: kvs'
218
+ in
238
219
W. debug " create_row %s (%s) [%s]" tblname new_objref
239
220
(String. concat " ,"
240
221
(List. map (fun (k , _ ) -> Printf. sprintf " (%s,v)" k) kvs')
241
222
) ;
242
223
create_row_locked t tblname kvs' new_objref
243
224
)
244
225
226
+ let create_row t tblname kvs' new_objref =
227
+ let kvs' =
228
+ List. map
229
+ (fun (key , value ) ->
230
+ let value =
231
+ match value with
232
+ | Schema.Value. String x ->
233
+ Schema.Value. String (ensure_utf8_xml_and_share x)
234
+ | Schema.Value. Pairs ps ->
235
+ Schema.Value. Pairs
236
+ (List. map
237
+ (fun (x , y ) ->
238
+ (ensure_utf8_xml_and_share x, ensure_utf8_xml_and_share y)
239
+ )
240
+ ps
241
+ )
242
+ | Schema.Value. Set xs ->
243
+ Schema.Value. Set (List. map ensure_utf8_xml_and_share xs)
244
+ in
245
+ (key, Schema.CachedValue. v value)
246
+ )
247
+ kvs'
248
+ in
249
+ create_row' t tblname kvs' new_objref
250
+
245
251
(* Do linear scan to find field values which match where clause *)
246
- let read_field_where t rcd =
252
+ let read_field_where' conv t rcd =
247
253
let db = get_database t in
248
254
let tbl = TableSet. find rcd.table (Database. tableset db) in
249
255
Table. fold
250
256
(fun _ _ row acc ->
251
- let field = Schema.Value. marshal (Row. find rcd.where_field row) in
257
+ let field =
258
+ Schema.CachedValue. string_of (Row. find' rcd.where_field row)
259
+ in
252
260
if field = rcd.where_value then
253
- Schema.Value. marshal (Row. find rcd.return row) :: acc
261
+ conv (Row. find' rcd.return row) :: acc
254
262
else
255
263
acc
256
264
)
257
265
tbl []
258
266
267
+ let read_field_where t rcd = read_field_where' Fun. id t rcd
268
+
259
269
let db_get_by_uuid t tbl uuid_val =
260
270
match
261
- read_field_where t
271
+ read_field_where' Schema.CachedValue. string_of t
262
272
{
263
273
table= tbl
264
274
; return= Db_names. ref
@@ -275,7 +285,7 @@ let db_get_by_uuid t tbl uuid_val =
275
285
276
286
let db_get_by_uuid_opt t tbl uuid_val =
277
287
match
278
- read_field_where t
288
+ read_field_where' Schema.CachedValue. string_of t
279
289
{
280
290
table= tbl
281
291
; return= Db_names. ref
@@ -290,7 +300,7 @@ let db_get_by_uuid_opt t tbl uuid_val =
290
300
291
301
(* * Return reference fields from tbl that matches specified name_label field *)
292
302
let db_get_by_name_label t tbl label =
293
- read_field_where t
303
+ read_field_where' Schema.CachedValue. string_of t
294
304
{
295
305
table= tbl
296
306
; return= Db_names. ref
@@ -324,11 +334,14 @@ let find_refs_with_filter_internal db (tblname : Db_interface.table)
324
334
325
335
let find_refs_with_filter t = find_refs_with_filter_internal (get_database t)
326
336
327
- let read_records_where t tbl expr =
337
+ let read_records_where' conv t tbl expr =
328
338
let db = get_database t in
329
339
let reqd_refs = find_refs_with_filter_internal db tbl expr in
330
340
if ! fist_delay_read_records_where then Thread. delay 0.5 ;
331
- List. map (fun ref -> (ref , read_record_internal db tbl ref )) reqd_refs
341
+ List. map (fun ref -> (ref , read_record_internal conv db tbl ref )) reqd_refs
342
+
343
+ let read_records_where t tbl expr =
344
+ read_records_where' Schema.CachedValue. open_present t tbl expr
332
345
333
346
let process_structured_field_locked t (key , value ) tblname fld objref
334
347
proc_fn_selector =
@@ -369,8 +382,8 @@ let process_structured_field_locked t (key, value) tblname fld objref
369
382
let process_structured_field t (key , value ) tblname fld objref proc_fn_selector
370
383
=
371
384
(* Ensure that both keys and values are valid for UTF-8-encoded XML. *)
372
- let key = ensure_utf8_xml key |> Share. merge in
373
- let value = ensure_utf8_xml value |> Share. merge in
385
+ let key = ensure_utf8_xml_and_share key in
386
+ let value = ensure_utf8_xml_and_share value in
374
387
with_lock (fun () ->
375
388
process_structured_field_locked t (key, value) tblname fld objref
376
389
proc_fn_selector
@@ -530,3 +543,41 @@ let stats t =
530
543
)
531
544
(Database. tableset (get_database t))
532
545
[]
546
+
547
+ module Compat = struct
548
+ type field_in = string
549
+
550
+ type field_out = string
551
+
552
+ let read_field_where t rcd =
553
+ read_field_where' Schema.CachedValue. string_of t rcd
554
+
555
+ let read_field t tblname fldname objref =
556
+ read_field t tblname fldname objref |> Schema.CachedValue. string_of
557
+
558
+ let write_field t tblname objref fldname newval =
559
+ let db = get_database t in
560
+ let schema = Schema. table tblname (Database. schema db) in
561
+ let column = Schema.Table. find fldname schema in
562
+ let newval = Schema.Value. unmarshal column.Schema.Column. ty newval in
563
+ write_field t tblname objref fldname newval
564
+
565
+ let read_record t =
566
+ read_record_internal Schema.CachedValue. string_of (get_database t)
567
+
568
+ let read_records_where t tbl expr =
569
+ read_records_where' Schema.CachedValue. string_of t tbl expr
570
+
571
+ let create_row t tblname kvs' new_objref =
572
+ let db = get_database t in
573
+ let schema = Schema. table tblname (Database. schema db) in
574
+ let kvs' =
575
+ List. map
576
+ (fun (key , value ) ->
577
+ let column = Schema.Table. find key schema in
578
+ (key, Schema.CachedValue. of_typed_string column.Schema.Column. ty value)
579
+ )
580
+ kvs'
581
+ in
582
+ create_row' t tblname kvs' new_objref
583
+ end
0 commit comments