{"action":"created","comment":{"url":"https://api.github.com/repos/influxdata/influxdb/pulls/comments/71787829","id":71787829,"diff_hunk":"@@ -1,92 +1,143 @@\n package main\n \n import (\n-\t\"bufio\"\n+\t\"compress/gzip\"\n \t\"fmt\"\n-\t\"io/ioutil\"\n+\t\"io\"\n+\t\"log\"\n \t\"os\"\n \t\"path/filepath\"\n \t\"strings\"\n \n \t\"github.com/influxdata/influxdb/tsdb/engine/tsm1\"\n )\n \n-func cmdExport(path string) {\n-\tdataPath := filepath.Join(path, \"data\")\n+type cmdExport struct {\n+\tpath            string\n+\tout             string\n+\tdatabase        string\n+\tretentionPolicy string\n+\tcompress        bool\n \n-\t// No need to do this in a loop\n-\text := fmt.Sprintf(\".%s\", tsm1.TSMFileExtension)\n+\text   string\n+\tfiles map[string][]string\n+}\n \n-\t// Get all TSM files by walking through the data dir\n-\tfiles := []string{}\n-\terr := filepath.Walk(dataPath, func(path string, f os.FileInfo, err error) error {\n-\t\tif err != nil {\n-\t\t\treturn err\n-\t\t}\n-\t\tif filepath.Ext(path) == ext {\n-\t\t\tfiles = append(files, path)\n-\t\t}\n-\t\treturn nil\n-\t})\n-\tif err != nil {\n-\t\tpanic(err)\n+func newCmdExport(path, out, database, retentionPolicy string, compress bool) *cmdExport {\n+\treturn &cmdExport{\n+\t\tpath:            filepath.Join(path, \"data\"),\n+\t\tout:             out,\n+\t\tdatabase:        database,\n+\t\tcompress:        compress,\n+\t\text:             fmt.Sprintf(\".%s\", tsm1.TSMFileExtension),\n+\t\tretentionPolicy: retentionPolicy,\n+\t\tfiles:           make(map[string][]string),\n \t}\n+}\n \n-\t// Loop through each file and output all of the data\n-\tfor _, f := range files {\n-\t\tfile, err := os.OpenFile(f, os.O_RDONLY, 0600)\n+func (c *cmdExport) validate() error {\n+\t// validate args\n+\tif c.retentionPolicy != \"\" && c.database == \"\" {\n+\t\treturn fmt.Errorf(\"must specify a database\")\n+\t}\n+\treturn nil\n+}\n \n-\t\trelPath, _ := filepath.Rel(dataPath, f)\n-\t\tdirs := strings.Split(relPath, string(byte(os.PathSeparator)))\n-\t\tdatabase := dirs[0]\n-\t\tfmt.Println(\"Opening\", f, \"in database\", database)\n-\t\ttemp, _ := ioutil.TempDir(\"/tmp\", \"influxdb\")\n+func (c *cmdExport) run() error {\n+\tif err := c.validate(); err != nil {\n+\t\treturn err\n+\t}\n \n-\t\tif err != nil {\n-\t\t\tfmt.Printf(\"%v\", err)\n-\t\t\tos.Exit(1)\n-\t\t}\n+\treturn c.export()\n+}\n \n-\t\treader, err := tsm1.NewTSMReader(file)\n+func (c *cmdExport) export() error {\n+\tif err := c.walkFiles(); err != nil {\n+\t\treturn err\n+\t}\n+\treturn c.writeFiles()\n+}\n \n+func (c *cmdExport) walkFiles() error {\n+\terr := filepath.Walk(c.path, func(path string, f os.FileInfo, err error) error {\n \t\tif err != nil {\n-\t\t\tfmt.Printf(\"%v\", err)\n-\t\t\tos.Exit(1)\n+\t\t\treturn err\n \t\t}\n-\n-\t\tfilename := temp + \"/\" + database + \".lp\"\n-\n-\t\tof, _ := os.Create(filename)\n-\t\tdefer of.Close()\n-\t\tw := bufio.NewWriter(of)\n-\n-\t\tfmt.Println(\"Writing\", reader.KeyCount(), \"series to\", filename)\n-\n-\t\tfor i := 0; i < reader.KeyCount(); i++ {\n-\t\t\tvar pairs string\n-\t\t\tkey, typ := reader.KeyAt(i)\n-\t\t\tvalues, _ := reader.ReadAll(key)\n-\t\t\tsplit := strings.Split(key, \"#!~#\")\n-\t\t\tmeasurement, field := split[0], split[1]\n-\n-\t\t\tfor _, value := range values {\n-\t\t\t\tswitch typ {\n-\t\t\t\tcase tsm1.BlockFloat64:\n-\t\t\t\t\tpairs = field + \"=\" + fmt.Sprintf(\"%v\", value.Value())\n-\t\t\t\tcase tsm1.BlockInteger:\n-\t\t\t\t\tpairs = field + \"=\" + fmt.Sprintf(\"%vi\", value.Value())\n-\t\t\t\tcase tsm1.BlockBoolean:\n-\t\t\t\t\tpairs = field + \"=\" + fmt.Sprintf(\"%v\", value.Value())\n-\t\t\t\tcase tsm1.BlockString:\n-\t\t\t\t\tpairs = field + \"=\" + fmt.Sprintf(\"\\\"%v\\\"\", value.Value())\n-\t\t\t\tdefault:\n-\t\t\t\t\tpairs = field + \"=\" + fmt.Sprintf(\"%v\", value.Value())\n+\t\tif filepath.Ext(path) == c.ext {\n+\t\t\t//files = append(files, path)\n+\t\t\trelPath, _ := filepath.Rel(c.path, path)\n+\t\t\tdirs := strings.Split(relPath, string(byte(os.PathSeparator)))\n+\t\t\tif dirs[0] == c.database || c.database == \"\" {\n+\t\t\t\tif dirs[1] == c.retentionPolicy || c.retentionPolicy == \"\" {\n+\t\t\t\t\tkey := filepath.Join(dirs[0], dirs[1])\n+\t\t\t\t\tfiles := c.files[key]\n+\t\t\t\t\tif files == nil {\n+\t\t\t\t\t\tfiles = []string{}\n+\t\t\t\t\t}\n+\t\t\t\t\tc.files[key] = append(files, path)\n \t\t\t\t}\n-\n-\t\t\t\tfmt.Fprintln(w, measurement, pairs, value.UnixNano())\n \t\t\t}\n+\t\t}\n+\t\treturn nil\n+\t})\n+\tif err != nil {\n+\t\tlog.Fatal(err)\n+\t}\n+\treturn nil\n+}\n \n-\t\t\tw.Flush()\n+func (c *cmdExport) writeFiles() error {\n+\t// open our output file and create an output buffer\n+\tvar w io.WriteCloser\n+\tw, _ = os.Create(c.out)\n+\tdefer w.Close()\n+\tif c.compress {\n+\t\tw = gzip.NewWriter(w)\n+\t}\n+\tfmt.Fprintln(w, \"# DML\")","path":"cmd/influx_inspect/export.go","position":165,"original_position":162,"commit_id":"a26d3dab656ac815e5be40f3d602b8a305711a23","original_commit_id":"5dc40db49d93327d9f4923048adaecda1793806f","user":{"login":"corylanou","id":585100,"avatar_url":"https://avatars.githubusercontent.com/u/585100?v=3","gravatar_id":"","url":"https://api.github.com/users/corylanou","html_url":"https://github.com/corylanou","followers_url":"https://api.github.com/users/corylanou/followers","following_url":"https://api.github.com/users/corylanou/following{/other_user}","gists_url":"https://api.github.com/users/corylanou/gists{/gist_id}","starred_url":"https://api.github.com/users/corylanou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/corylanou/subscriptions","organizations_url":"https://api.github.com/users/corylanou/orgs","repos_url":"https://api.github.com/users/corylanou/repos","events_url":"https://api.github.com/users/corylanou/events{/privacy}","received_events_url":"https://api.github.com/users/corylanou/received_events","type":"User","site_admin":false},"body":"We don't know the policy for the RP though...  I think it will err out if it doesn't match.","created_at":"2016-07-21T21:11:11Z","updated_at":"2016-07-21T21:11:11Z","html_url":"https://github.com/influxdata/influxdb/pull/7046#discussion_r71787829","pull_request_url":"https://api.github.com/repos/influxdata/influxdb/pulls/7046","_links":{"self":{"href":"https://api.github.com/repos/influxdata/influxdb/pulls/comments/71787829"},"html":{"href":"https://github.com/influxdata/influxdb/pull/7046#discussion_r71787829"},"pull_request":{"href":"https://api.github.com/repos/influxdata/influxdb/pulls/7046"}}},"pull_request":{"url":"https://api.github.com/repos/influxdata/influxdb/pulls/7046","id":78382688,"html_url":"https://github.com/influxdata/influxdb/pull/7046","diff_url":"https://github.com/influxdata/influxdb/pull/7046.diff","patch_url":"https://github.com/influxdata/influxdb/pull/7046.patch","issue_url":"https://api.github.com/repos/influxdata/influxdb/issues/7046","number":7046,"state":"open","locked":false,"title":"Finish tsm file exporter","user":{"login":"corylanou","id":585100,"avatar_url":"https://avatars.githubusercontent.com/u/585100?v=3","gravatar_id":"","url":"https://api.github.com/users/corylanou","html_url":"https://github.com/corylanou","followers_url":"https://api.github.com/users/corylanou/followers","following_url":"https://api.github.com/users/corylanou/following{/other_user}","gists_url":"https://api.github.com/users/corylanou/gists{/gist_id}","starred_url":"https://api.github.com/users/corylanou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/corylanou/subscriptions","organizations_url":"https://api.github.com/users/corylanou/orgs","repos_url":"https://api.github.com/users/corylanou/repos","events_url":"https://api.github.com/users/corylanou/events{/privacy}","received_events_url":"https://api.github.com/users/corylanou/received_events","type":"User","site_admin":false},"body":"Add ability to export TSM files to line protocol to be imported back into another database.\r\n\r\n- [ ] Rebased/mergable\r\n- [ ] Tests pass\r\n- [ ] CHANGELOG.md updated\r\n- [ ] Update docs\r\n- [ ] Update README.md\r\n\r\n","created_at":"2016-07-21T20:34:54Z","updated_at":"2016-07-21T21:11:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"42df8c4e9016b94b4aebb9cc68303917c816b0f2","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/influxdata/influxdb/pulls/7046/commits","review_comments_url":"https://api.github.com/repos/influxdata/influxdb/pulls/7046/comments","review_comment_url":"https://api.github.com/repos/influxdata/influxdb/pulls/comments{/number}","comments_url":"https://api.github.com/repos/influxdata/influxdb/issues/7046/comments","statuses_url":"https://api.github.com/repos/influxdata/influxdb/statuses/a26d3dab656ac815e5be40f3d602b8a305711a23","head":{"label":"influxdata:cjl-tsm-export","ref":"cjl-tsm-export","sha":"a26d3dab656ac815e5be40f3d602b8a305711a23","user":{"login":"influxdata","id":5713248,"avatar_url":"https://avatars.githubusercontent.com/u/5713248?v=3","gravatar_id":"","url":"https://api.github.com/users/influxdata","html_url":"https://github.com/influxdata","followers_url":"https://api.github.com/users/influxdata/followers","following_url":"https://api.github.com/users/influxdata/following{/other_user}","gists_url":"https://api.github.com/users/influxdata/gists{/gist_id}","starred_url":"https://api.github.com/users/influxdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/influxdata/subscriptions","organizations_url":"https://api.github.com/users/influxdata/orgs","repos_url":"https://api.github.com/users/influxdata/repos","events_url":"https://api.github.com/users/influxdata/events{/privacy}","received_events_url":"https://api.github.com/users/influxdata/received_events","type":"Organization","site_admin":false},"repo":{"id":13124802,"name":"influxdb","full_name":"influxdata/influxdb","owner":{"login":"influxdata","id":5713248,"avatar_url":"https://avatars.githubusercontent.com/u/5713248?v=3","gravatar_id":"","url":"https://api.github.com/users/influxdata","html_url":"https://github.com/influxdata","followers_url":"https://api.github.com/users/influxdata/followers","following_url":"https://api.github.com/users/influxdata/following{/other_user}","gists_url":"https://api.github.com/users/influxdata/gists{/gist_id}","starred_url":"https://api.github.com/users/influxdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/influxdata/subscriptions","organizations_url":"https://api.github.com/users/influxdata/orgs","repos_url":"https://api.github.com/users/influxdata/repos","events_url":"https://api.github.com/users/influxdata/events{/privacy}","received_events_url":"https://api.github.com/users/influxdata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/influxdata/influxdb","description":"Scalable datastore for metrics, events, and real-time analytics","fork":false,"url":"https://api.github.com/repos/influxdata/influxdb","forks_url":"https://api.github.com/repos/influxdata/influxdb/forks","keys_url":"https://api.github.com/repos/influxdata/influxdb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/influxdata/influxdb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/influxdata/influxdb/teams","hooks_url":"https://api.github.com/repos/influxdata/influxdb/hooks","issue_events_url":"https://api.github.com/repos/influxdata/influxdb/issues/events{/number}","events_url":"https://api.github.com/repos/influxdata/influxdb/events","assignees_url":"https://api.github.com/repos/influxdata/influxdb/assignees{/user}","branches_url":"https://api.github.com/repos/influxdata/influxdb/branches{/branch}","tags_url":"https://api.github.com/repos/influxdata/influxdb/tags","blobs_url":"https://api.github.com/repos/influxdata/influxdb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/influxdata/influxdb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/influxdata/influxdb/git/refs{/sha}","trees_url":"https://api.github.com/repos/influxdata/influxdb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/influxdata/influxdb/statuses/{sha}","languages_url":"https://api.github.com/repos/influxdata/influxdb/languages","stargazers_url":"https://api.github.com/repos/influxdata/influxdb/stargazers","contributors_url":"https://api.github.com/repos/influxdata/influxdb/contributors","subscribers_url":"https://api.github.com/repos/influxdata/influxdb/subscribers","subscription_url":"https://api.github.com/repos/influxdata/influxdb/subscription","commits_url":"https://api.github.com/repos/influxdata/influxdb/commits{/sha}","git_commits_url":"https://api.github.com/repos/influxdata/influxdb/git/commits{/sha}","comments_url":"https://api.github.com/repos/influxdata/influxdb/comments{/number}","issue_comment_url":"https://api.github.com/repos/influxdata/influxdb/issues/comments{/number}","contents_url":"https://api.github.com/repos/influxdata/influxdb/contents/{+path}","compare_url":"https://api.github.com/repos/influxdata/influxdb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/influxdata/influxdb/merges","archive_url":"https://api.github.com/repos/influxdata/influxdb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/influxdata/influxdb/downloads","issues_url":"https://api.github.com/repos/influxdata/influxdb/issues{/number}","pulls_url":"https://api.github.com/repos/influxdata/influxdb/pulls{/number}","milestones_url":"https://api.github.com/repos/influxdata/influxdb/milestones{/number}","notifications_url":"https://api.github.com/repos/influxdata/influxdb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/influxdata/influxdb/labels{/name}","releases_url":"https://api.github.com/repos/influxdata/influxdb/releases{/id}","deployments_url":"https://api.github.com/repos/influxdata/influxdb/deployments","created_at":"2013-09-26T14:31:10Z","updated_at":"2016-07-21T17:25:26Z","pushed_at":"2016-07-21T21:10:19Z","git_url":"git://github.com/influxdata/influxdb.git","ssh_url":"git@github.com:influxdata/influxdb.git","clone_url":"https://github.com/influxdata/influxdb.git","svn_url":"https://github.com/influxdata/influxdb","homepage":"https://influxdata.com","size":59606,"stargazers_count":8472,"watchers_count":8472,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1180,"mirror_url":null,"open_issues_count":442,"forks":1180,"open_issues":442,"watchers":8472,"default_branch":"master"}},"base":{"label":"influxdata:master","ref":"master","sha":"da10220016e7049b0490a43c8c06ea8988d1bc1d","user":{"login":"influxdata","id":5713248,"avatar_url":"https://avatars.githubusercontent.com/u/5713248?v=3","gravatar_id":"","url":"https://api.github.com/users/influxdata","html_url":"https://github.com/influxdata","followers_url":"https://api.github.com/users/influxdata/followers","following_url":"https://api.github.com/users/influxdata/following{/other_user}","gists_url":"https://api.github.com/users/influxdata/gists{/gist_id}","starred_url":"https://api.github.com/users/influxdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/influxdata/subscriptions","organizations_url":"https://api.github.com/users/influxdata/orgs","repos_url":"https://api.github.com/users/influxdata/repos","events_url":"https://api.github.com/users/influxdata/events{/privacy}","received_events_url":"https://api.github.com/users/influxdata/received_events","type":"Organization","site_admin":false},"repo":{"id":13124802,"name":"influxdb","full_name":"influxdata/influxdb","owner":{"login":"influxdata","id":5713248,"avatar_url":"https://avatars.githubusercontent.com/u/5713248?v=3","gravatar_id":"","url":"https://api.github.com/users/influxdata","html_url":"https://github.com/influxdata","followers_url":"https://api.github.com/users/influxdata/followers","following_url":"https://api.github.com/users/influxdata/following{/other_user}","gists_url":"https://api.github.com/users/influxdata/gists{/gist_id}","starred_url":"https://api.github.com/users/influxdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/influxdata/subscriptions","organizations_url":"https://api.github.com/users/influxdata/orgs","repos_url":"https://api.github.com/users/influxdata/repos","events_url":"https://api.github.com/users/influxdata/events{/privacy}","received_events_url":"https://api.github.com/users/influxdata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/influxdata/influxdb","description":"Scalable datastore for metrics, events, and real-time analytics","fork":false,"url":"https://api.github.com/repos/influxdata/influxdb","forks_url":"https://api.github.com/repos/influxdata/influxdb/forks","keys_url":"https://api.github.com/repos/influxdata/influxdb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/influxdata/influxdb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/influxdata/influxdb/teams","hooks_url":"https://api.github.com/repos/influxdata/influxdb/hooks","issue_events_url":"https://api.github.com/repos/influxdata/influxdb/issues/events{/number}","events_url":"https://api.github.com/repos/influxdata/influxdb/events","assignees_url":"https://api.github.com/repos/influxdata/influxdb/assignees{/user}","branches_url":"https://api.github.com/repos/influxdata/influxdb/branches{/branch}","tags_url":"https://api.github.com/repos/influxdata/influxdb/tags","blobs_url":"https://api.github.com/repos/influxdata/influxdb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/influxdata/influxdb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/influxdata/influxdb/git/refs{/sha}","trees_url":"https://api.github.com/repos/influxdata/influxdb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/influxdata/influxdb/statuses/{sha}","languages_url":"https://api.github.com/repos/influxdata/influxdb/languages","stargazers_url":"https://api.github.com/repos/influxdata/influxdb/stargazers","contributors_url":"https://api.github.com/repos/influxdata/influxdb/contributors","subscribers_url":"https://api.github.com/repos/influxdata/influxdb/subscribers","subscription_url":"https://api.github.com/repos/influxdata/influxdb/subscription","commits_url":"https://api.github.com/repos/influxdata/influxdb/commits{/sha}","git_commits_url":"https://api.github.com/repos/influxdata/influxdb/git/commits{/sha}","comments_url":"https://api.github.com/repos/influxdata/influxdb/comments{/number}","issue_comment_url":"https://api.github.com/repos/influxdata/influxdb/issues/comments{/number}","contents_url":"https://api.github.com/repos/influxdata/influxdb/contents/{+path}","compare_url":"https://api.github.com/repos/influxdata/influxdb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/influxdata/influxdb/merges","archive_url":"https://api.github.com/repos/influxdata/influxdb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/influxdata/influxdb/downloads","issues_url":"https://api.github.com/repos/influxdata/influxdb/issues{/number}","pulls_url":"https://api.github.com/repos/influxdata/influxdb/pulls{/number}","milestones_url":"https://api.github.com/repos/influxdata/influxdb/milestones{/number}","notifications_url":"https://api.github.com/repos/influxdata/influxdb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/influxdata/influxdb/labels{/name}","releases_url":"https://api.github.com/repos/influxdata/influxdb/releases{/id}","deployments_url":"https://api.github.com/repos/influxdata/influxdb/deployments","created_at":"2013-09-26T14:31:10Z","updated_at":"2016-07-21T17:25:26Z","pushed_at":"2016-07-21T21:10:19Z","git_url":"git://github.com/influxdata/influxdb.git","ssh_url":"git@github.com:influxdata/influxdb.git","clone_url":"https://github.com/influxdata/influxdb.git","svn_url":"https://github.com/influxdata/influxdb","homepage":"https://influxdata.com","size":59606,"stargazers_count":8472,"watchers_count":8472,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1180,"mirror_url":null,"open_issues_count":442,"forks":1180,"open_issues":442,"watchers":8472,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/influxdata/influxdb/pulls/7046"},"html":{"href":"https://github.com/influxdata/influxdb/pull/7046"},"issue":{"href":"https://api.github.com/repos/influxdata/influxdb/issues/7046"},"comments":{"href":"https://api.github.com/repos/influxdata/influxdb/issues/7046/comments"},"review_comments":{"href":"https://api.github.com/repos/influxdata/influxdb/pulls/7046/comments"},"review_comment":{"href":"https://api.github.com/repos/influxdata/influxdb/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/influxdata/influxdb/pulls/7046/commits"},"statuses":{"href":"https://api.github.com/repos/influxdata/influxdb/statuses/a26d3dab656ac815e5be40f3d602b8a305711a23"}}}}
{"actor":{"display_login":"corylanou"}}