{"id":13411923,"url":"https://github.com/amit-davidson/LibraDB","last_synced_at":"2025-03-14T17:31:17.688Z","repository":{"id":144346837,"uuid":"400846406","full_name":"amit-davidson/LibraDB","owner":"amit-davidson","description":"LibraDB is a simple, persistent key/value store written in pure Go in less than 1000 lines for learning purposes.","archived":false,"fork":false,"pushed_at":"2024-04-11T18:39:36.000Z","size":93,"stargazers_count":147,"open_issues_count":2,"forks_count":17,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-04-22T13:31:55.500Z","etag":null,"topics":["data-structures","database","go","golang"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amit-davidson.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":"2021-08-28T17:02:46.000Z","updated_at":"2024-07-30T22:52:41.172Z","dependencies_parsed_at":"2024-07-30T23:09:23.189Z","dependency_job_id":null,"html_url":"https://github.com/amit-davidson/LibraDB","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amit-davidson%2FLibraDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amit-davidson%2FLibraDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amit-davidson%2FLibraDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amit-davidson%2FLibraDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amit-davidson","download_url":"https://codeload.github.com/amit-davidson/LibraDB/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243618692,"owners_count":20320276,"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","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":["data-structures","database","go","golang"],"created_at":"2024-07-30T20:01:18.614Z","updated_at":"2025-03-14T17:31:14.547Z","avatar_url":"https://github.com/amit-davidson.png","language":"Go","funding_links":[],"categories":["Database","数据库","Data Integration Frameworks"],"sub_categories":["Databases Implemented in Go","Go中实现的数据库"],"readme":"# LibraDB\n\n[![made-with-Go](https://github.com/go-critic/go-critic/workflows/Go/badge.svg)](http://golang.org)\n[![made-with-Go](https://img.shields.io/badge/Made%20with-Go-1f425f.svg)](http://golang.org)\n[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://lbesson.mit-license.org/)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)\n[![amit-davidson](https://circleci.com/gh/amit-davidson/LibraDB.svg?style=svg)](https://app.circleci.com/pipelines/github/amit-davidson/LibraDB)\n\nLibraDB is a simple, persistent key/value store written in pure Go. The project aims to provide a working yet simple\nexample of a working database. If you're interested in databases, I encourage you to start here.\n\nThis database accompanies  my [blog post](https://betterprogramming.pub/build-a-nosql-database-from-the-scratch-in-1000-lines-of-code-8ed1c15ed924) on how to write a database from scratch.\n\n## Installing\n\nTo start using LibraDB, install Go and run `go get`:\n\n```sh\ngo get -u github.com/amit-davidson/LibraDB\n```\n\n## Basic usage\n```go\npackage main\n\nimport \"github.com/amit-davidson/LibraDB\"\n\nfunc main() {\n\tpath := \"libra.db\"\n\tdb, _ := LibraDB.Open(path, LibraDB.DefaultOptions)\n\n\ttx := db.WriteTx()\n\tname := []byte(\"test\")\n\tcollection, _ := tx.CreateCollection(name)\n\n\tkey, value := []byte(\"key1\"), []byte(\"value1\")\n\t_ = collection.Put(key, value)\n\n\t_ = tx.Commit()\n}\n```\n## Transactions\nRead-only and read-write transactions are supported. LibraDB allows multiple read transactions or one read-write \ntransaction at the same time. Transactions are goroutine-safe.\n\nLibraDB has an isolation level: [Serializable](https://en.wikipedia.org/wiki/Isolation_(database_systems)#Serializable).\nIn simpler words, transactions are executed one after another and not at the same time.This is the highest isolation level.\n\n### Read-write transactions\n\n```go\ntx := db.WriteTx()\n...\nif err := tx.Commit(); err != nil {\n    return err\n}\n```\n### Read-only transactions\n```go\ntx := db.ReadTx()\n...\nif err := tx.Commit(); err != nil {\n    return err\n}\n```\n\n## Collections\nCollections are a grouping of key-value pairs. Collections are used to organize and quickly access data as each\ncollection is B-Tree by itself. All keys in a collection must be unique.\n```go\ntx := db.WriteTx()\ncollection, err := tx.CreateCollection([]byte(\"test\"))\nif err != nil {\n\treturn err\n}\n_ = tx.Commit()\n```\n\n### Auto generating ID\nThe `Collection.ID()` function returns an integer to be used as a unique identifier for key/value pairs.\n```go\ntx := db.WriteTx()\ncollection, err := tx.GetCollection([]byte(\"test\"))\nif err != nil {\n    return err\n}\nid := collection.ID()\n_ = tx.Commit()\n```\n## Key-Value Pairs\nKey/value pairs reside inside collections. CRUD operations are possible using the methods `Collection.Put` \n`Collection.Find` `Collection.Remove` as shown below.   \n```go\ntx := db.WriteTx()\ncollection, err := tx.GetCollection([]byte(\"test\"))\nif  err != nil {\n    return err\n}\n\nkey, value := []byte(\"key1\"), []byte(\"value1\")\nif err := collection.Put(key, value); err != nil {\n    return err\n}\nif item, err := collection.Find(key); err != nil {\n    return err\n}\n\nif err := collection.Remove(key); err != nil {\n    return err\n}\n_ = tx.Commit()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famit-davidson%2FLibraDB","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famit-davidson%2FLibraDB","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famit-davidson%2FLibraDB/lists"}