{"action":"created","comment":{"url":"https://api.github.com/repos/eclipse/smarthome/pulls/comments/116949631","pull_request_review_id":38611166,"id":116949631,"diff_hunk":"@@ -261,23 +261,33 @@ private File getBackupFile(int age) {\n     }\n \n     /**\n-     * Write out any outstanding data\n+     * Write out any outstanding data.\n+     * <p>\n+     * This creates the backup copy at the same time as writing the database file. This avoids\n+     * having to either rename the file later (which may leave a small window for there to\n+     * be no file if the system crashes during the write process), or to copy the file when\n+     * writing the backup copy (which would require a read and write, and is thus slower).\n      */\n     public void commitDatabase() {\n         String s = mapper.toJson(map);\n \n         synchronized (map) {\n-            // Rename the file for backup\n-            File rename = new File(file.getParent() + File.separator + BACKUP_EXTENSION,\n-                    System.currentTimeMillis() + SEPARATOR + file.getName());\n-            file.renameTo(rename);\n-\n+            // Write the database file\n             try (FileOutputStream outputStream = new FileOutputStream(file, false);) {\n                 outputStream.write(s.getBytes());\n             } catch (Exception e) {\n                 logger.error(\"Error writing JsonDB to {}. Cause {}.\", file.getPath(), e.getMessage());\n             }\n \n+            // And also write the backup\n+            File backup = new File(file.getParent() + File.separator + BACKUP_EXTENSION,\n+                    System.currentTimeMillis() + SEPARATOR + file.getName());\n+            try (FileOutputStream outputStream = new FileOutputStream(backup, false);) {","path":"bundles/storage/org.eclipse.smarthome.storage.json/src/main/java/org/eclipse/smarthome/storage/json/JsonStorage.java","position":42,"original_position":42,"commit_id":"26edd662692b98d50a4257479bc1f6cd695c8f35","original_commit_id":"26edd662692b98d50a4257479bc1f6cd695c8f35","user":{"login":"sjka","id":2592443,"avatar_url":"https://avatars2.githubusercontent.com/u/2592443?v=3","gravatar_id":"","url":"https://api.github.com/users/sjka","html_url":"https://github.com/sjka","followers_url":"https://api.github.com/users/sjka/followers","following_url":"https://api.github.com/users/sjka/following{/other_user}","gists_url":"https://api.github.com/users/sjka/gists{/gist_id}","starred_url":"https://api.github.com/users/sjka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sjka/subscriptions","organizations_url":"https://api.github.com/users/sjka/orgs","repos_url":"https://api.github.com/users/sjka/repos","events_url":"https://api.github.com/users/sjka/events{/privacy}","received_events_url":"https://api.github.com/users/sjka/received_events","type":"User","site_admin":false},"body":"As these lines for actually writing out the file are identical for `file` and `backup` (apart of this parameter, of course), could you please factor them out?","created_at":"2017-05-17T09:15:49Z","updated_at":"2017-05-17T09:19:08Z","html_url":"https://github.com/eclipse/smarthome/pull/3439#discussion_r116949631","pull_request_url":"https://api.github.com/repos/eclipse/smarthome/pulls/3439","_links":{"self":{"href":"https://api.github.com/repos/eclipse/smarthome/pulls/comments/116949631"},"html":{"href":"https://github.com/eclipse/smarthome/pull/3439#discussion_r116949631"},"pull_request":{"href":"https://api.github.com/repos/eclipse/smarthome/pulls/3439"}}},"pull_request":{"url":"https://api.github.com/repos/eclipse/smarthome/pulls/3439","id":120329308,"html_url":"https://github.com/eclipse/smarthome/pull/3439","diff_url":"https://github.com/eclipse/smarthome/pull/3439.diff","patch_url":"https://github.com/eclipse/smarthome/pull/3439.patch","issue_url":"https://api.github.com/repos/eclipse/smarthome/issues/3439","number":3439,"state":"open","locked":false,"title":"Write jsondb backup without renaming original file.","user":{"login":"cdjackson","id":1041475,"avatar_url":"https://avatars1.githubusercontent.com/u/1041475?v=3","gravatar_id":"","url":"https://api.github.com/users/cdjackson","html_url":"https://github.com/cdjackson","followers_url":"https://api.github.com/users/cdjackson/followers","following_url":"https://api.github.com/users/cdjackson/following{/other_user}","gists_url":"https://api.github.com/users/cdjackson/gists{/gist_id}","starred_url":"https://api.github.com/users/cdjackson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cdjackson/subscriptions","organizations_url":"https://api.github.com/users/cdjackson/orgs","repos_url":"https://api.github.com/users/cdjackson/repos","events_url":"https://api.github.com/users/cdjackson/events{/privacy}","received_events_url":"https://api.github.com/users/cdjackson/received_events","type":"User","site_admin":false},"body":"This changes the way the backup is written. The backup is now written at the same time as the data is stored, rather than storing a single file, and then having to rename it later. The main database file is overwritten so should not ever be removed unless the underlying filesystem is doing this (in which case there is probably nothing that can be done at Java level?).\r\n\r\nThis avoids the situation where there is no file for any period of time, and this should ensure the backup is read if the main file is corrupted due to a crash (etc).\r\n\r\nWriting the backup at the same time as the main file avoids a (slower) read->write which would be required to copy the original file if it was done later.\r\n\r\nThis has another (arguable) advantage in that there are now two copies of the latest file, so two chances to load with the latest data.\r\n\r\nFixes #3433\r\nSigned-off-by: Chris Jackson <chris@cd-jackson.com>","created_at":"2017-05-12T13:59:01Z","updated_at":"2017-05-17T09:19:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":"245194a3d769632e1ae6255593793536fc4a207c","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/eclipse/smarthome/pulls/3439/commits","review_comments_url":"https://api.github.com/repos/eclipse/smarthome/pulls/3439/comments","review_comment_url":"https://api.github.com/repos/eclipse/smarthome/pulls/comments{/number}","comments_url":"https://api.github.com/repos/eclipse/smarthome/issues/3439/comments","statuses_url":"https://api.github.com/repos/eclipse/smarthome/statuses/26edd662692b98d50a4257479bc1f6cd695c8f35","head":{"label":"cdjackson:jsondb-write","ref":"jsondb-write","sha":"26edd662692b98d50a4257479bc1f6cd695c8f35","user":{"login":"cdjackson","id":1041475,"avatar_url":"https://avatars1.githubusercontent.com/u/1041475?v=3","gravatar_id":"","url":"https://api.github.com/users/cdjackson","html_url":"https://github.com/cdjackson","followers_url":"https://api.github.com/users/cdjackson/followers","following_url":"https://api.github.com/users/cdjackson/following{/other_user}","gists_url":"https://api.github.com/users/cdjackson/gists{/gist_id}","starred_url":"https://api.github.com/users/cdjackson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cdjackson/subscriptions","organizations_url":"https://api.github.com/users/cdjackson/orgs","repos_url":"https://api.github.com/users/cdjackson/repos","events_url":"https://api.github.com/users/cdjackson/events{/privacy}","received_events_url":"https://api.github.com/users/cdjackson/received_events","type":"User","site_admin":false},"repo":{"id":17073319,"name":"smarthome","full_name":"cdjackson/smarthome","owner":{"login":"cdjackson","id":1041475,"avatar_url":"https://avatars1.githubusercontent.com/u/1041475?v=3","gravatar_id":"","url":"https://api.github.com/users/cdjackson","html_url":"https://github.com/cdjackson","followers_url":"https://api.github.com/users/cdjackson/followers","following_url":"https://api.github.com/users/cdjackson/following{/other_user}","gists_url":"https://api.github.com/users/cdjackson/gists{/gist_id}","starred_url":"https://api.github.com/users/cdjackson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cdjackson/subscriptions","organizations_url":"https://api.github.com/users/cdjackson/orgs","repos_url":"https://api.github.com/users/cdjackson/repos","events_url":"https://api.github.com/users/cdjackson/events{/privacy}","received_events_url":"https://api.github.com/users/cdjackson/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/cdjackson/smarthome","description":"Eclipse SmartHome project","fork":true,"url":"https://api.github.com/repos/cdjackson/smarthome","forks_url":"https://api.github.com/repos/cdjackson/smarthome/forks","keys_url":"https://api.github.com/repos/cdjackson/smarthome/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cdjackson/smarthome/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cdjackson/smarthome/teams","hooks_url":"https://api.github.com/repos/cdjackson/smarthome/hooks","issue_events_url":"https://api.github.com/repos/cdjackson/smarthome/issues/events{/number}","events_url":"https://api.github.com/repos/cdjackson/smarthome/events","assignees_url":"https://api.github.com/repos/cdjackson/smarthome/assignees{/user}","branches_url":"https://api.github.com/repos/cdjackson/smarthome/branches{/branch}","tags_url":"https://api.github.com/repos/cdjackson/smarthome/tags","blobs_url":"https://api.github.com/repos/cdjackson/smarthome/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cdjackson/smarthome/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cdjackson/smarthome/git/refs{/sha}","trees_url":"https://api.github.com/repos/cdjackson/smarthome/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cdjackson/smarthome/statuses/{sha}","languages_url":"https://api.github.com/repos/cdjackson/smarthome/languages","stargazers_url":"https://api.github.com/repos/cdjackson/smarthome/stargazers","contributors_url":"https://api.github.com/repos/cdjackson/smarthome/contributors","subscribers_url":"https://api.github.com/repos/cdjackson/smarthome/subscribers","subscription_url":"https://api.github.com/repos/cdjackson/smarthome/subscription","commits_url":"https://api.github.com/repos/cdjackson/smarthome/commits{/sha}","git_commits_url":"https://api.github.com/repos/cdjackson/smarthome/git/commits{/sha}","comments_url":"https://api.github.com/repos/cdjackson/smarthome/comments{/number}","issue_comment_url":"https://api.github.com/repos/cdjackson/smarthome/issues/comments{/number}","contents_url":"https://api.github.com/repos/cdjackson/smarthome/contents/{+path}","compare_url":"https://api.github.com/repos/cdjackson/smarthome/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cdjackson/smarthome/merges","archive_url":"https://api.github.com/repos/cdjackson/smarthome/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cdjackson/smarthome/downloads","issues_url":"https://api.github.com/repos/cdjackson/smarthome/issues{/number}","pulls_url":"https://api.github.com/repos/cdjackson/smarthome/pulls{/number}","milestones_url":"https://api.github.com/repos/cdjackson/smarthome/milestones{/number}","notifications_url":"https://api.github.com/repos/cdjackson/smarthome/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cdjackson/smarthome/labels{/name}","releases_url":"https://api.github.com/repos/cdjackson/smarthome/releases{/id}","deployments_url":"https://api.github.com/repos/cdjackson/smarthome/deployments","created_at":"2014-02-21T23:24:18Z","updated_at":"2016-01-23T18:32:58Z","pushed_at":"2017-05-12T13:44:57Z","git_url":"git://github.com/cdjackson/smarthome.git","ssh_url":"git@github.com:cdjackson/smarthome.git","clone_url":"https://github.com/cdjackson/smarthome.git","svn_url":"https://github.com/cdjackson/smarthome","homepage":null,"size":28082,"stargazers_count":2,"watchers_count":2,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":2,"default_branch":"master"}},"base":{"label":"eclipse:master","ref":"master","sha":"0a24a97d82d39a942d5d0ac9cc0f91282c537f2f","user":{"login":"eclipse","id":56974,"avatar_url":"https://avatars2.githubusercontent.com/u/56974?v=3","gravatar_id":"","url":"https://api.github.com/users/eclipse","html_url":"https://github.com/eclipse","followers_url":"https://api.github.com/users/eclipse/followers","following_url":"https://api.github.com/users/eclipse/following{/other_user}","gists_url":"https://api.github.com/users/eclipse/gists{/gist_id}","starred_url":"https://api.github.com/users/eclipse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eclipse/subscriptions","organizations_url":"https://api.github.com/users/eclipse/orgs","repos_url":"https://api.github.com/users/eclipse/repos","events_url":"https://api.github.com/users/eclipse/events{/privacy}","received_events_url":"https://api.github.com/users/eclipse/received_events","type":"Organization","site_admin":false},"repo":{"id":15778981,"name":"smarthome","full_name":"eclipse/smarthome","owner":{"login":"eclipse","id":56974,"avatar_url":"https://avatars2.githubusercontent.com/u/56974?v=3","gravatar_id":"","url":"https://api.github.com/users/eclipse","html_url":"https://github.com/eclipse","followers_url":"https://api.github.com/users/eclipse/followers","following_url":"https://api.github.com/users/eclipse/following{/other_user}","gists_url":"https://api.github.com/users/eclipse/gists{/gist_id}","starred_url":"https://api.github.com/users/eclipse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eclipse/subscriptions","organizations_url":"https://api.github.com/users/eclipse/orgs","repos_url":"https://api.github.com/users/eclipse/repos","events_url":"https://api.github.com/users/eclipse/events{/privacy}","received_events_url":"https://api.github.com/users/eclipse/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/eclipse/smarthome","description":"Eclipse SmartHome™ project","fork":false,"url":"https://api.github.com/repos/eclipse/smarthome","forks_url":"https://api.github.com/repos/eclipse/smarthome/forks","keys_url":"https://api.github.com/repos/eclipse/smarthome/keys{/key_id}","collaborators_url":"https://api.github.com/repos/eclipse/smarthome/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/eclipse/smarthome/teams","hooks_url":"https://api.github.com/repos/eclipse/smarthome/hooks","issue_events_url":"https://api.github.com/repos/eclipse/smarthome/issues/events{/number}","events_url":"https://api.github.com/repos/eclipse/smarthome/events","assignees_url":"https://api.github.com/repos/eclipse/smarthome/assignees{/user}","branches_url":"https://api.github.com/repos/eclipse/smarthome/branches{/branch}","tags_url":"https://api.github.com/repos/eclipse/smarthome/tags","blobs_url":"https://api.github.com/repos/eclipse/smarthome/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/eclipse/smarthome/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/eclipse/smarthome/git/refs{/sha}","trees_url":"https://api.github.com/repos/eclipse/smarthome/git/trees{/sha}","statuses_url":"https://api.github.com/repos/eclipse/smarthome/statuses/{sha}","languages_url":"https://api.github.com/repos/eclipse/smarthome/languages","stargazers_url":"https://api.github.com/repos/eclipse/smarthome/stargazers","contributors_url":"https://api.github.com/repos/eclipse/smarthome/contributors","subscribers_url":"https://api.github.com/repos/eclipse/smarthome/subscribers","subscription_url":"https://api.github.com/repos/eclipse/smarthome/subscription","commits_url":"https://api.github.com/repos/eclipse/smarthome/commits{/sha}","git_commits_url":"https://api.github.com/repos/eclipse/smarthome/git/commits{/sha}","comments_url":"https://api.github.com/repos/eclipse/smarthome/comments{/number}","issue_comment_url":"https://api.github.com/repos/eclipse/smarthome/issues/comments{/number}","contents_url":"https://api.github.com/repos/eclipse/smarthome/contents/{+path}","compare_url":"https://api.github.com/repos/eclipse/smarthome/compare/{base}...{head}","merges_url":"https://api.github.com/repos/eclipse/smarthome/merges","archive_url":"https://api.github.com/repos/eclipse/smarthome/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/eclipse/smarthome/downloads","issues_url":"https://api.github.com/repos/eclipse/smarthome/issues{/number}","pulls_url":"https://api.github.com/repos/eclipse/smarthome/pulls{/number}","milestones_url":"https://api.github.com/repos/eclipse/smarthome/milestones{/number}","notifications_url":"https://api.github.com/repos/eclipse/smarthome/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/eclipse/smarthome/labels{/name}","releases_url":"https://api.github.com/repos/eclipse/smarthome/releases{/id}","deployments_url":"https://api.github.com/repos/eclipse/smarthome/deployments","created_at":"2014-01-09T20:47:19Z","updated_at":"2017-05-16T14:14:26Z","pushed_at":"2017-05-17T08:40:06Z","git_url":"git://github.com/eclipse/smarthome.git","ssh_url":"git@github.com:eclipse/smarthome.git","clone_url":"https://github.com/eclipse/smarthome.git","svn_url":"https://github.com/eclipse/smarthome","homepage":"https://www.eclipse.org/smarthome/","size":29428,"stargazers_count":490,"watchers_count":490,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":473,"mirror_url":null,"open_issues_count":306,"forks":473,"open_issues":306,"watchers":490,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/eclipse/smarthome/pulls/3439"},"html":{"href":"https://github.com/eclipse/smarthome/pull/3439"},"issue":{"href":"https://api.github.com/repos/eclipse/smarthome/issues/3439"},"comments":{"href":"https://api.github.com/repos/eclipse/smarthome/issues/3439/comments"},"review_comments":{"href":"https://api.github.com/repos/eclipse/smarthome/pulls/3439/comments"},"review_comment":{"href":"https://api.github.com/repos/eclipse/smarthome/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/eclipse/smarthome/pulls/3439/commits"},"statuses":{"href":"https://api.github.com/repos/eclipse/smarthome/statuses/26edd662692b98d50a4257479bc1f6cd695c8f35"}}}}
{"actor":{"display_login":"sjka"}}