ovsdb: New function ovsdb_file_read_schema() for reading schema from db.
[cascardo/ovs.git] / ovsdb / file.c
1 /* Copyright (c) 2009, 2010, 2011 Nicira Networks
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17
18 #include "file.h"
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24
25 #include "bitmap.h"
26 #include "column.h"
27 #include "log.h"
28 #include "json.h"
29 #include "lockfile.h"
30 #include "ovsdb.h"
31 #include "ovsdb-error.h"
32 #include "row.h"
33 #include "socket-util.h"
34 #include "table.h"
35 #include "timeval.h"
36 #include "transaction.h"
37 #include "uuid.h"
38 #include "util.h"
39 #include "vlog.h"
40
41 VLOG_DEFINE_THIS_MODULE(ovsdb_file);
42
43 /* Minimum number of milliseconds between database compactions. */
44 #define COMPACT_MIN_MSEC        (10 * 60 * 1000) /* 10 minutes. */
45
46 /* Minimum number of milliseconds between trying to compact the database if
47  * compacting fails. */
48 #define COMPACT_RETRY_MSEC      (60 * 1000)      /* 1 minute. */
49
50 /* A transaction being converted to JSON for writing to a file. */
51 struct ovsdb_file_txn {
52     struct json *json;          /* JSON for the whole transaction. */
53     struct json *table_json;    /* JSON for 'table''s transaction. */
54     struct ovsdb_table *table;  /* Table described in 'table_json'.  */
55 };
56
57 static void ovsdb_file_txn_init(struct ovsdb_file_txn *);
58 static void ovsdb_file_txn_add_row(struct ovsdb_file_txn *,
59                                    const struct ovsdb_row *old,
60                                    const struct ovsdb_row *new,
61                                    const unsigned long int *changed);
62 static struct ovsdb_error *ovsdb_file_txn_commit(struct json *,
63                                                  const char *comment,
64                                                  bool durable,
65                                                  struct ovsdb_log *);
66
67 static struct ovsdb_error *ovsdb_file_open__(const char *file_name,
68                                              const struct ovsdb_schema *,
69                                              bool read_only, struct ovsdb **,
70                                              struct ovsdb_file **);
71 static struct ovsdb_error *ovsdb_file_txn_from_json(
72     struct ovsdb *, const struct json *, bool converting,
73     long long int *date, struct ovsdb_txn **);
74 static struct ovsdb_error *ovsdb_file_create(struct ovsdb *,
75                                              struct ovsdb_log *,
76                                              const char *file_name,
77                                              long long int oldest_commit,
78                                              unsigned int n_transactions,
79                                              struct ovsdb_file **filep);
80
81 /* Opens database 'file_name' and stores a pointer to the new database in
82  * '*dbp'.  If 'read_only' is false, then the database will be locked and
83  * changes to the database will be written to disk.  If 'read_only' is true,
84  * the database will not be locked and changes to the database will persist
85  * only as long as the "struct ovsdb".
86  *
87  * If 'filep' is nonnull and 'read_only' is false, then on success sets
88  * '*filep' to an ovsdb_file that represents the open file.  This ovsdb_file
89  * persists until '*dbp' is destroyed.
90  *
91  * On success, returns NULL.  On failure, returns an ovsdb_error (which the
92  * caller must destroy) and sets '*dbp' and '*filep' to NULL. */
93 struct ovsdb_error *
94 ovsdb_file_open(const char *file_name, bool read_only,
95                 struct ovsdb **dbp, struct ovsdb_file **filep)
96 {
97     return ovsdb_file_open__(file_name, NULL, read_only, dbp, filep);
98 }
99
100 /* Opens database 'file_name' with an alternate schema.  The specified 'schema'
101  * is used to interpret the data in 'file_name', ignoring the schema actually
102  * stored in the file.  Data in the file for tables or columns that do not
103  * exist in 'schema' are ignored, but the ovsdb file format must otherwise be
104  * observed, including column constraints.
105  *
106  * This function can be useful for upgrading or downgrading databases to
107  * "almost-compatible" formats.
108  *
109  * The database will not be locked.  Changes to the database will persist only
110  * as long as the "struct ovsdb".
111  *
112  * On success, stores a pointer to the new database in '*dbp' and returns a
113  * null pointer.  On failure, returns an ovsdb_error (which the caller must
114  * destroy) and sets '*dbp' to NULL. */
115 struct ovsdb_error *
116 ovsdb_file_open_as_schema(const char *file_name,
117                           const struct ovsdb_schema *schema,
118                           struct ovsdb **dbp)
119 {
120     return ovsdb_file_open__(file_name, schema, true, dbp, NULL);
121 }
122
123 static struct ovsdb_error *
124 ovsdb_file_open_log(const char *file_name, enum ovsdb_log_open_mode open_mode,
125                     struct ovsdb_log **logp, struct ovsdb_schema **schemap)
126 {
127     struct ovsdb_schema *schema = NULL;
128     struct ovsdb_log *log = NULL;
129     struct ovsdb_error *error;
130     struct json *json = NULL;
131
132     assert(logp || schemap);
133
134     error = ovsdb_log_open(file_name, open_mode, -1, &log);
135     if (error) {
136         goto error;
137     }
138
139     error = ovsdb_log_read(log, &json);
140     if (error) {
141         goto error;
142     } else if (!json) {
143         error = ovsdb_io_error(EOF, "%s: database file contains no schema",
144                                file_name);
145         goto error;
146     }
147
148     if (schemap) {
149         error = ovsdb_schema_from_json(json, &schema);
150         if (error) {
151             json_destroy(json);
152             error = ovsdb_wrap_error(error,
153                                      "failed to parse \"%s\" as ovsdb schema",
154                                      file_name);
155             goto error;
156         }
157     }
158     json_destroy(json);
159
160     if (logp) {
161         *logp = log;
162     } else {
163         ovsdb_log_close(log);
164     }
165     if (schemap) {
166         *schemap = schema;
167     }
168     return NULL;
169
170 error:
171     ovsdb_log_close(log);
172     json_destroy(json);
173     if (logp) {
174         *logp = NULL;
175     }
176     if (schemap) {
177         *schemap = NULL;
178     }
179     return error;
180 }
181
182 static struct ovsdb_error *
183 ovsdb_file_open__(const char *file_name,
184                   const struct ovsdb_schema *alternate_schema,
185                   bool read_only, struct ovsdb **dbp,
186                   struct ovsdb_file **filep)
187 {
188     enum ovsdb_log_open_mode open_mode;
189     long long int oldest_commit;
190     unsigned int n_transactions;
191     struct ovsdb_schema *schema = NULL;
192     struct ovsdb_error *error;
193     struct ovsdb_log *log;
194     struct json *json;
195     struct ovsdb *db = NULL;
196
197     /* In read-only mode there is no ovsdb_file so 'filep' must be null. */
198     assert(!(read_only && filep));
199
200     open_mode = read_only ? OVSDB_LOG_READ_ONLY : OVSDB_LOG_READ_WRITE;
201     error = ovsdb_file_open_log(file_name, open_mode, &log,
202                                 alternate_schema ? NULL : &schema);
203     if (error) {
204         goto error;
205     }
206
207     db = ovsdb_create(schema ? schema : ovsdb_schema_clone(alternate_schema));
208
209     oldest_commit = LLONG_MAX;
210     n_transactions = 0;
211     while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
212         struct ovsdb_txn *txn;
213         long long int date;
214
215         error = ovsdb_file_txn_from_json(db, json, alternate_schema != NULL,
216                                          &date, &txn);
217         json_destroy(json);
218         if (error) {
219             break;
220         }
221
222         n_transactions++;
223         if (date < oldest_commit) {
224             oldest_commit = date;
225         }
226
227         ovsdb_txn_commit(txn, false);
228     }
229     if (error) {
230         /* Log error but otherwise ignore it.  Probably the database just got
231          * truncated due to power failure etc. and we should use its current
232          * contents. */
233         char *msg = ovsdb_error_to_string(error);
234         VLOG_WARN("%s", msg);
235         free(msg);
236
237         ovsdb_error_destroy(error);
238     }
239
240     if (!read_only) {
241         struct ovsdb_file *file;
242
243         error = ovsdb_file_create(db, log, file_name, oldest_commit,
244                                   n_transactions, &file);
245         if (error) {
246             goto error;
247         }
248         if (filep) {
249             *filep = file;
250         }
251     } else {
252         ovsdb_log_close(log);
253     }
254
255     *dbp = db;
256     return NULL;
257
258 error:
259     *dbp = NULL;
260     if (filep) {
261         *filep = NULL;
262     }
263     ovsdb_destroy(db);
264     ovsdb_log_close(log);
265     return error;
266 }
267
268 static struct ovsdb_error *
269 ovsdb_file_update_row_from_json(struct ovsdb_row *row, bool converting,
270                                 const struct json *json)
271 {
272     struct ovsdb_table_schema *schema = row->table->schema;
273     struct ovsdb_error *error;
274     struct shash_node *node;
275
276     if (json->type != JSON_OBJECT) {
277         return ovsdb_syntax_error(json, NULL, "row must be JSON object");
278     }
279
280     SHASH_FOR_EACH (node, json_object(json)) {
281         const char *column_name = node->name;
282         const struct ovsdb_column *column;
283         struct ovsdb_datum datum;
284
285         column = ovsdb_table_schema_get_column(schema, column_name);
286         if (!column) {
287             if (converting) {
288                 continue;
289             }
290             return ovsdb_syntax_error(json, "unknown column",
291                                       "No column %s in table %s.",
292                                       column_name, schema->name);
293         }
294
295         error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
296         if (error) {
297             return error;
298         }
299         ovsdb_datum_swap(&row->fields[column->index], &datum);
300         ovsdb_datum_destroy(&datum, &column->type);
301     }
302
303     return NULL;
304 }
305
306 static struct ovsdb_error *
307 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
308                              bool converting,
309                              const struct uuid *row_uuid, struct json *json)
310 {
311     const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
312     if (json->type == JSON_NULL) {
313         if (!row) {
314             return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
315                                       "row "UUID_FMT" that does not exist",
316                                       UUID_ARGS(row_uuid));
317         }
318         ovsdb_txn_row_delete(txn, row);
319         return NULL;
320     } else if (row) {
321         return ovsdb_file_update_row_from_json(ovsdb_txn_row_modify(txn, row),
322                                                converting, json);
323     } else {
324         struct ovsdb_error *error;
325         struct ovsdb_row *new;
326
327         new = ovsdb_row_create(table);
328         *ovsdb_row_get_uuid_rw(new) = *row_uuid;
329         error = ovsdb_file_update_row_from_json(new, converting, json);
330         if (error) {
331             ovsdb_row_destroy(new);
332         }
333
334         ovsdb_txn_row_insert(txn, new);
335
336         return error;
337     }
338 }
339
340 static struct ovsdb_error *
341 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
342                                struct ovsdb_table *table,
343                                bool converting, struct json *json)
344 {
345     struct shash_node *node;
346
347     if (json->type != JSON_OBJECT) {
348         return ovsdb_syntax_error(json, NULL, "object expected");
349     }
350
351     SHASH_FOR_EACH (node, json->u.object) {
352         const char *uuid_string = node->name;
353         struct json *txn_row_json = node->data;
354         struct ovsdb_error *error;
355         struct uuid row_uuid;
356
357         if (!uuid_from_string(&row_uuid, uuid_string)) {
358             return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
359                                       uuid_string);
360         }
361
362         error = ovsdb_file_txn_row_from_json(txn, table, converting,
363                                              &row_uuid, txn_row_json);
364         if (error) {
365             return error;
366         }
367     }
368
369     return NULL;
370 }
371
372 /* Converts 'json' to an ovsdb_txn for 'db', storing the new transaction in
373  * '*txnp'.  Returns NULL if successful, otherwise an error.
374  *
375  * If 'converting' is true, then unknown table and column names are ignored
376  * (which can ease upgrading and downgrading schemas); otherwise, they are
377  * treated as errors.
378  *
379  * If successful, the date associated with the transaction, as the number of
380  * milliseconds since the epoch, is stored in '*date'.  If the transaction does
381  * not include a date, LLONG_MAX is stored. */
382 static struct ovsdb_error *
383 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
384                          bool converting, long long int *date,
385                          struct ovsdb_txn **txnp)
386 {
387     struct ovsdb_error *error;
388     struct shash_node *node;
389     struct ovsdb_txn *txn;
390
391     *txnp = NULL;
392     *date = LLONG_MAX;
393
394     if (json->type != JSON_OBJECT) {
395         return ovsdb_syntax_error(json, NULL, "object expected");
396     }
397
398     txn = ovsdb_txn_create(db);
399     SHASH_FOR_EACH (node, json->u.object) {
400         const char *table_name = node->name;
401         struct json *node_json = node->data;
402         struct ovsdb_table *table;
403
404         table = shash_find_data(&db->tables, table_name);
405         if (!table) {
406             if (!strcmp(table_name, "_date")
407                 && node_json->type == JSON_INTEGER) {
408                 if (date) {
409                     *date = json_integer(node_json);
410                 }
411                 continue;
412             } else if (!strcmp(table_name, "_comment") || converting) {
413                 continue;
414             }
415
416             error = ovsdb_syntax_error(json, "unknown table",
417                                        "No table named %s.", table_name);
418             goto error;
419         }
420
421         error = ovsdb_file_txn_table_from_json(txn, table, converting,
422                                                node_json);
423         if (error) {
424             goto error;
425         }
426     }
427     *txnp = txn;
428     return NULL;
429
430 error:
431     ovsdb_txn_abort(txn);
432     return error;
433 }
434
435 static struct ovsdb_error *
436 ovsdb_file_save_copy__(const char *file_name, int locking,
437                        const char *comment, const struct ovsdb *db,
438                        struct ovsdb_log **logp)
439 {
440     const struct shash_node *node;
441     struct ovsdb_file_txn ftxn;
442     struct ovsdb_error *error;
443     struct ovsdb_log *log;
444     struct json *json;
445
446     error = ovsdb_log_open(file_name, OVSDB_LOG_CREATE, locking, &log);
447     if (error) {
448         return error;
449     }
450
451     /* Write schema. */
452     json = ovsdb_schema_to_json(db->schema);
453     error = ovsdb_log_write(log, json);
454     json_destroy(json);
455     if (error) {
456         goto exit;
457     }
458
459     /* Write data. */
460     ovsdb_file_txn_init(&ftxn);
461     SHASH_FOR_EACH (node, &db->tables) {
462         const struct ovsdb_table *table = node->data;
463         const struct ovsdb_row *row;
464
465         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
466             ovsdb_file_txn_add_row(&ftxn, NULL, row, NULL);
467         }
468     }
469     error = ovsdb_file_txn_commit(ftxn.json, comment, true, log);
470
471 exit:
472     if (logp) {
473         if (!error) {
474             *logp = log;
475             log = NULL;
476         } else {
477             *logp = NULL;
478         }
479     }
480     ovsdb_log_close(log);
481     if (error) {
482         remove(file_name);
483     }
484     return error;
485 }
486
487 /* Saves a snapshot of 'db''s current contents as 'file_name'.  If 'comment' is
488  * nonnull, then it is added along with the data contents and can be viewed
489  * with "ovsdb-tool show-log".
490  *
491  * 'locking' is passed along to ovsdb_log_open() untouched. */
492 struct ovsdb_error *
493 ovsdb_file_save_copy(const char *file_name, int locking,
494                      const char *comment, const struct ovsdb *db)
495 {
496     return ovsdb_file_save_copy__(file_name, locking, comment, db, NULL);
497 }
498
499 /* Opens database 'file_name', reads its schema, and closes it.  On success,
500  * stores the schema into '*schemap' and returns NULL; the caller then owns the
501  * schema.  On failure, returns an ovsdb_error (which the caller must destroy)
502  * and sets '*dbp' to NULL. */
503 struct ovsdb_error *
504 ovsdb_file_read_schema(const char *file_name, struct ovsdb_schema **schemap)
505 {
506     assert(schemap != NULL);
507     return ovsdb_file_open_log(file_name, OVSDB_LOG_READ_ONLY, NULL, schemap);
508 }
509 \f
510 /* Replica implementation. */
511
512 struct ovsdb_file {
513     struct ovsdb_replica replica;
514     struct ovsdb *db;
515     struct ovsdb_log *log;
516     char *file_name;
517     long long int oldest_commit;
518     long long int next_compact;
519     unsigned int n_transactions;
520 };
521
522 static const struct ovsdb_replica_class ovsdb_file_class;
523
524 static struct ovsdb_error *
525 ovsdb_file_create(struct ovsdb *db, struct ovsdb_log *log,
526                   const char *file_name,
527                   long long int oldest_commit,
528                   unsigned int n_transactions,
529                   struct ovsdb_file **filep)
530 {
531     long long int now = time_msec();
532     struct ovsdb_file *file;
533     char *abs_name;
534
535     /* Use the absolute name of the file because ovsdb-server opens its
536      * database before daemonize() chdirs to "/". */
537     abs_name = abs_file_name(NULL, file_name);
538     if (!abs_name) {
539         *filep = NULL;
540         return ovsdb_io_error(0, "could not determine current "
541                               "working directory");
542     }
543
544     file = xmalloc(sizeof *file);
545     ovsdb_replica_init(&file->replica, &ovsdb_file_class);
546     file->db = db;
547     file->log = log;
548     file->file_name = abs_name;
549     file->oldest_commit = MIN(oldest_commit, now);
550     file->next_compact = file->oldest_commit + COMPACT_MIN_MSEC;
551     file->n_transactions = n_transactions;
552     ovsdb_add_replica(db, &file->replica);
553
554     *filep = file;
555     return NULL;
556 }
557
558 static struct ovsdb_file *
559 ovsdb_file_cast(struct ovsdb_replica *replica)
560 {
561     assert(replica->class == &ovsdb_file_class);
562     return CONTAINER_OF(replica, struct ovsdb_file, replica);
563 }
564
565 static bool
566 ovsdb_file_change_cb(const struct ovsdb_row *old,
567                      const struct ovsdb_row *new,
568                      const unsigned long int *changed,
569                      void *ftxn_)
570 {
571     struct ovsdb_file_txn *ftxn = ftxn_;
572     ovsdb_file_txn_add_row(ftxn, old, new, changed);
573     return true;
574 }
575
576 static struct ovsdb_error *
577 ovsdb_file_commit(struct ovsdb_replica *replica,
578                   const struct ovsdb_txn *txn, bool durable)
579 {
580     struct ovsdb_file *file = ovsdb_file_cast(replica);
581     struct ovsdb_file_txn ftxn;
582     struct ovsdb_error *error;
583
584     ovsdb_file_txn_init(&ftxn);
585     ovsdb_txn_for_each_change(txn, ovsdb_file_change_cb, &ftxn);
586     if (!ftxn.json) {
587         /* Nothing to commit. */
588         return NULL;
589     }
590
591     error = ovsdb_file_txn_commit(ftxn.json, ovsdb_txn_get_comment(txn),
592                                   durable, file->log);
593     if (error) {
594         return error;
595     }
596     file->n_transactions++;
597
598     /* If it has been at least COMPACT_MIN_MSEC millseconds since the last time
599      * we compacted (or at least COMPACT_RETRY_MSEC since the last time we
600      * tried), and if there are at least 100 transactions in the database, and
601      * if the database is at least 1 MB, then compact the database. */
602     if (time_msec() >= file->next_compact
603         && file->n_transactions >= 100
604         && ovsdb_log_get_offset(file->log) >= 10 * 1024 * 1024)
605     {
606         error = ovsdb_file_compact(file);
607         if (error) {
608             char *s = ovsdb_error_to_string(error);
609             ovsdb_error_destroy(error);
610             VLOG_WARN("%s: compacting database failed (%s), retrying in "
611                       "60 seconds", file->file_name, s);
612             free(s);
613
614             file->next_compact = time_msec() + COMPACT_RETRY_MSEC;
615         }
616     }
617
618     return NULL;
619 }
620
621 struct ovsdb_error *
622 ovsdb_file_compact(struct ovsdb_file *file)
623 {
624     struct ovsdb_log *new_log = NULL;
625     struct lockfile *tmp_lock = NULL;
626     struct ovsdb_error *error;
627     char *tmp_name = NULL;
628     char *comment = NULL;
629     int retval;
630
631     comment = xasprintf("compacting database online "
632                         "(%.3f seconds old, %u transactions, %llu bytes)",
633                         (time_msec() - file->oldest_commit) / 1000.0,
634                         file->n_transactions,
635                         (unsigned long long) ovsdb_log_get_offset(file->log));
636     VLOG_INFO("%s: %s", file->file_name, comment);
637
638     /* Commit the old version, so that we can be assured that we'll eventually
639      * have either the old or the new version. */
640     error = ovsdb_log_commit(file->log);
641     if (error) {
642         goto exit;
643     }
644
645     /* Lock temporary file. */
646     tmp_name = xasprintf("%s.tmp", file->file_name);
647     retval = lockfile_lock(tmp_name, 0, &tmp_lock);
648     if (retval) {
649         error = ovsdb_io_error(retval, "could not get lock on %s", tmp_name);
650         goto exit;
651     }
652
653     /* Remove temporary file.  (It might not exist.) */
654     if (unlink(tmp_name) < 0 && errno != ENOENT) {
655         error = ovsdb_io_error(errno, "failed to remove %s", tmp_name);
656         goto exit;
657     }
658
659     /* Save a copy. */
660     error = ovsdb_file_save_copy__(tmp_name, false, comment, file->db,
661                                    &new_log);
662     if (error) {
663         goto exit;
664     }
665
666     /* Replace original by temporary. */
667     if (rename(tmp_name, file->file_name)) {
668         error = ovsdb_io_error(errno, "failed to rename \"%s\" to \"%s\"",
669                                tmp_name, file->file_name);
670         goto exit;
671     }
672     fsync_parent_dir(file->file_name);
673
674 exit:
675     if (!error) {
676         ovsdb_log_close(file->log);
677         file->log = new_log;
678         file->oldest_commit = time_msec();
679         file->next_compact = file->oldest_commit + COMPACT_MIN_MSEC;
680         file->n_transactions = 1;
681     } else {
682         ovsdb_log_close(new_log);
683         if (tmp_lock) {
684             unlink(tmp_name);
685         }
686     }
687
688     lockfile_unlock(tmp_lock);
689     free(tmp_name);
690     free(comment);
691
692     return error;
693 }
694
695 static void
696 ovsdb_file_destroy(struct ovsdb_replica *replica)
697 {
698     struct ovsdb_file *file = ovsdb_file_cast(replica);
699
700     ovsdb_log_close(file->log);
701     free(file->file_name);
702     free(file);
703 }
704
705 static const struct ovsdb_replica_class ovsdb_file_class = {
706     ovsdb_file_commit,
707     ovsdb_file_destroy
708 };
709 \f
710 static void
711 ovsdb_file_txn_init(struct ovsdb_file_txn *ftxn)
712 {
713     ftxn->json = NULL;
714     ftxn->table_json = NULL;
715     ftxn->table = NULL;
716 }
717
718 static void
719 ovsdb_file_txn_add_row(struct ovsdb_file_txn *ftxn,
720                        const struct ovsdb_row *old,
721                        const struct ovsdb_row *new,
722                        const unsigned long int *changed)
723 {
724     struct json *row;
725
726     if (!new) {
727         row = json_null_create();
728     } else {
729         struct shash_node *node;
730
731         row = old ? NULL : json_object_create();
732         SHASH_FOR_EACH (node, &new->table->schema->columns) {
733             const struct ovsdb_column *column = node->data;
734             const struct ovsdb_type *type = &column->type;
735             unsigned int idx = column->index;
736
737             if (idx != OVSDB_COL_UUID && column->persistent
738                 && (old
739                     ? bitmap_is_set(changed, idx)
740                     : !ovsdb_datum_is_default(&new->fields[idx], type)))
741             {
742                 if (!row) {
743                     row = json_object_create();
744                 }
745                 json_object_put(row, column->name,
746                                 ovsdb_datum_to_json(&new->fields[idx], type));
747             }
748         }
749     }
750
751     if (row) {
752         struct ovsdb_table *table = new ? new->table : old->table;
753         char uuid[UUID_LEN + 1];
754
755         if (table != ftxn->table) {
756             /* Create JSON object for transaction overall. */
757             if (!ftxn->json) {
758                 ftxn->json = json_object_create();
759             }
760
761             /* Create JSON object for transaction on this table. */
762             ftxn->table_json = json_object_create();
763             ftxn->table = table;
764             json_object_put(ftxn->json, table->schema->name, ftxn->table_json);
765         }
766
767         /* Add row to transaction for this table. */
768         snprintf(uuid, sizeof uuid,
769                  UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
770         json_object_put(ftxn->table_json, uuid, row);
771     }
772 }
773
774 static struct ovsdb_error *
775 ovsdb_file_txn_commit(struct json *json, const char *comment,
776                       bool durable, struct ovsdb_log *log)
777 {
778     struct ovsdb_error *error;
779
780     if (!json) {
781         json = json_object_create();
782     }
783     if (comment) {
784         json_object_put_string(json, "_comment", comment);
785     }
786     json_object_put(json, "_date", json_integer_create(time_wall()));
787
788     error = ovsdb_log_write(log, json);
789     json_destroy(json);
790     if (error) {
791         return ovsdb_wrap_error(error, "writing transaction failed");
792     }
793
794     if (durable) {
795         error = ovsdb_log_commit(log);
796         if (error) {
797             return ovsdb_wrap_error(error, "committing transaction failed");
798         }
799     }
800
801     return NULL;
802 }