{"id":31879972,"url":"https://github.com/l2060/snapsdb","last_synced_at":"2026-04-21T09:33:06.393Z","repository":{"id":104955548,"uuid":"540779031","full_name":"l2060/snapsdb","owner":"l2060","description":"golang objects snapshot slice data repository","archived":false,"fork":false,"pushed_at":"2022-10-19T01:20:05.000Z","size":66,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-27T04:16:57.509Z","etag":null,"topics":["database","slice","slicedatabase","snapsort","snapsortdb","strorage"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/l2060.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-24T09:40:57.000Z","updated_at":"2022-09-25T10:56:36.000Z","dependencies_parsed_at":"2023-06-04T16:00:13.826Z","dependency_job_id":null,"html_url":"https://github.com/l2060/snapsdb","commit_stats":null,"previous_names":["l2060/snapsdb"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/l2060/snapsdb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/l2060%2Fsnapsdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/l2060%2Fsnapsdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/l2060%2Fsnapsdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/l2060%2Fsnapsdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/l2060","download_url":"https://codeload.github.com/l2060/snapsdb/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/l2060%2Fsnapsdb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279013645,"owners_count":26085298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","slice","slicedatabase","snapsort","snapsortdb","strorage"],"created_at":"2025-10-13T00:55:19.315Z","updated_at":"2025-10-13T00:55:21.610Z","avatar_url":"https://github.com/l2060.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# snapsdb\ngolang  snapsort objects store database\n\nsnapsdb 是一款对象数据列表快照数据库，它诞生的目的是为了解决瞬间查询某些数据在历史上某一时刻的数据快照，\n它使用 protobuf 作为对象的序列化方式，这意味着您的对象必须由protoc指令创建，这样带来的好处是序列化性能的大幅度提升。\n\nsnapsdb是以时间线为单位的，每天会生成一个单独的文件。 在这个文件的开头存储着当天86400秒的所有时间线索引，这个索引分别是 first  last 两条记录，first负责数据查询读取，last负责新的数据写入。 这两个对象所指向的是一个单向链表，这样我们可以在任意时间存储任意时间线的数据。\n\n⚠️ 这个数据库不支持索引， 不支持数据聚合，目前它仅完成了数据写入和数据查询的功能。\n\n\n## use library💎\n``` bash\n go get -u github.com/vblegend/snapsdb\n```\n\n## 📦 look look test code 🤝\n\n``` golang\npackage test\n\nimport (\n\t\"fmt\"\n\t\"path/filepath\"\n\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/vblegend/snapsdb\"\n\t\"github.com/vblegend/snapsdb/test/types\"\n\t\"github.com/vblegend/snapsdb/util\"\n\n\t\"google.golang.org/protobuf/proto\"\n)\n\nfunc InitDB() snapsdb.SnapsDB {\n\tsnapPath := filepath.Join(util.AssemblyDir(), \"../snapdata/proc\")\n\tdb, err := snapsdb.InitDB(\n\t\tsnapsdb.WithDataPath(snapPath),\n\t\tsnapsdb.WithDataRetention(snapsdb.TimestampOf14Day),\n\t\tsnapsdb.WithTimeKeyFormat(\"2006-01-02 15:04:05\"),\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn db\n}\n\n// 测试 snapshotDB 批量插入\nfunc TestSnapshotDBWrite(_ *testing.T) {\n\tfmt.Println(\"开始测试\")\n\tdb := InitDB()\n\tv1 := \u0026types.ProcessInfo{Pid: 1, Name: \"docker-compose - 1\", Cpu: 10.01, Mem: 91.23, Virt: 10000000000, Res: 110000000000000}\n\tv2 := \u0026types.ProcessInfo{Pid: 2, Name: \"docker-compose - 2\", Cpu: 20.02, Mem: 92.34, Virt: 20000000000, Res: 220000000000000}\n\tv3 := \u0026types.ProcessInfo{Pid: 3, Name: \"docker-compose - 3\", Cpu: 30.03, Mem: 93.45, Virt: 30000000000, Res: 330000000000000}\n\tv4 := \u0026types.ProcessInfo{Pid: 4, Name: \"docker-compose - 4\", Cpu: 40.04, Mem: 94.56, Virt: 40000000000, Res: 440000000000000}\n\tv5 := \u0026types.ProcessInfo{Pid: 5, Name: \"docker-compose - 5\", Cpu: 50.05, Mem: 95.67, Virt: 50000000000, Res: 550000000000000}\n\n\tdata1, _ := proto.Marshal(v1)\n\tcount := 172800\n\tarray := []snapsdb.StoreData{v1, v2, v3, v4, v5}\n\n\tstart := time.Now() // 获取当前时间\n\tfor i := 0; i \u003c count; i++ {\n\t\ttimestamp := time.Date(2022, 9, 22, 0, 0, i, 0, time.Local)\n\t\tdb.Write(timestamp, array...)\n\t}\n\tcost := time.Since(start)\n\ttotal := count * len(array)\n\tbySec := float64(total) / cost.Seconds()\n\tfmt.Printf(\"写入完毕...\\n共计写入%s条数据\\n每条数据长度%s字节\\n单位数据量%s条\\n共用时%s\\n每秒写入量%s条\\n\",\n\t\tutil.Green(fmt.Sprintf(\"%d\", total)),\n\t\tutil.Green(fmt.Sprintf(\"%d\", len(data1))),\n\t\tutil.Green(fmt.Sprintf(\"%d\", len(array))),\n\t\tutil.Green(fmt.Sprintf(\"%v\", cost)),\n\t\tutil.Green(fmt.Sprintf(\"%.0f\", bySec)))\n}\n\n// 测试 snapshotDB 单条插入\nfunc TestSnapshotDBWriteOnce(t *testing.T) {\n\tfmt.Println(\"开始测试\")\n\tdb := InitDB()\n\tv1 := \u0026types.ProcessInfo{Pid: 1, Name: \"docker-compose - 1\", Cpu: 10.01, Mem: 91.23, Virt: 10000000000, Res: 110000000000000}\n\tv2 := \u0026types.ProcessInfo{Pid: 2, Name: \"docker-compose - 2\", Cpu: 20.02, Mem: 92.34, Virt: 20000000000, Res: 220000000000000}\n\tv3 := \u0026types.ProcessInfo{Pid: 3, Name: \"docker-compose - 3\", Cpu: 30.03, Mem: 93.45, Virt: 30000000000, Res: 330000000000000}\n\tv4 := \u0026types.ProcessInfo{Pid: 4, Name: \"docker-compose - 4\", Cpu: 40.04, Mem: 94.56, Virt: 40000000000, Res: 440000000000000}\n\tv5 := \u0026types.ProcessInfo{Pid: 5, Name: \"docker-compose - 5\", Cpu: 50.05, Mem: 95.67, Virt: 50000000000, Res: 550000000000000}\n\ttimestamp := time.Date(2022, 01, 01, 01, 01, 01, 01, time.Local)\n\tfmt.Println(timestamp.Format(\"2006-01-02 15:04:05\"))\n\tstart := time.Now() // 获取当前时间\n\tdb.Write(timestamp, v1, v2, v3, v4, v5)\n\tcost := time.Since(start)\n\tfmt.Printf(\"写入完毕，共计%d条数据，用时%v\\n\", 5, cost)\n}\n\n// 测试 snapshotDB 的时间线查询\nfunc TestSnapshotDBQuery(t *testing.T) {\n\tdb := InitDB()\n\ttimestamp := time.Date(2022, 9, 22, 13, 27, 43, 0, time.Local)\n\tlist := make([]types.ProcessInfo, 0)\n\tlist = append(list, types.ProcessInfo{Pid: 5, Name: \"docker-compose - 1111\", Cpu: 50.05, Mem: 95.67, Virt: 50000000000, Res: 550000000000000})\n\tstart := time.Now()\n\terr := db.QueryTimeline(timestamp, \u0026list)\n\tcost := time.Since(start)\n\tif err != nil {\n\t\tfmt.Println(util.Red(err.Error()))\n\t}\n\tfmt.Println(list)\n\tfmt.Printf(\"查询完毕，用时%v\\n\", cost)\n}\n\n// 测试 snapshotDB 的时间段查询\nfunc TestSnapshotDBQueryBetween(t *testing.T) {\n\tdb := InitDB()\n\tbeginTimestamp := time.Date(2022, 9, 22, 5, 0, 00, 0, time.Local)\n\tendTimestamp := time.Date(2022, 9, 22, 5, 2, 00, 0, time.Local)\n\toutmap := make(map[string][]types.ProcessInfo)\n\tstart := time.Now()\n\terr := db.QueryBetween(beginTimestamp, endTimestamp, \u0026outmap)\n\tcost := time.Since(start)\n\tif err != nil \u0026\u0026 err != snapsdb.ErrorDBFileNotHit {\n\t\tfmt.Println(util.Red(err.Error()))\n\t}\n\tfmt.Println(outmap)\n\tfmt.Printf(\"查询完毕，用时%v\\n\", cost)\n}\n\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fl2060%2Fsnapsdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fl2060%2Fsnapsdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fl2060%2Fsnapsdb/lists"}