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