• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..17-May-2021-

.gitignoreH A D17-May-2021273 2720

LICENSEH A D17-May-202115.5 KiB364265

README.mdH A D17-May-20213.9 KiB147121

changes.goH A D17-May-2021996 3517

filter.goH A D17-May-2021946 3420

go.modH A D17-May-2021159 96

go.sumH A D17-May-2021734 98

index.goH A D17-May-202124.1 KiB884621

memdb.goH A D17-May-20212.3 KiB9865

schema.goH A D17-May-20212.8 KiB11573

txn.goH A D17-May-202126.6 KiB941615

watch.goH A D17-May-20213.6 KiB14592

watch_few.goH A D17-May-20211.4 KiB11875

README.md

1# go-memdb [![CircleCI](https://circleci.com/gh/hashicorp/go-memdb/tree/master.svg?style=svg)](https://circleci.com/gh/hashicorp/go-memdb/tree/master)
2
3Provides the `memdb` package that implements a simple in-memory database
4built on immutable radix trees. The database provides Atomicity, Consistency
5and Isolation from ACID. Being that it is in-memory, it does not provide durability.
6The database is instantiated with a schema that specifies the tables and indices
7that exist and allows transactions to be executed.
8
9The database provides the following:
10
11* Multi-Version Concurrency Control (MVCC) - By leveraging immutable radix trees
12  the database is able to support any number of concurrent readers without locking,
13  and allows a writer to make progress.
14
15* Transaction Support - The database allows for rich transactions, in which multiple
16  objects are inserted, updated or deleted. The transactions can span multiple tables,
17  and are applied atomically. The database provides atomicity and isolation in ACID
18  terminology, such that until commit the updates are not visible.
19
20* Rich Indexing - Tables can support any number of indexes, which can be simple like
21  a single field index, or more advanced compound field indexes. Certain types like
22  UUID can be efficiently compressed from strings into byte indexes for reduced
23  storage requirements.
24
25* Watches - Callers can populate a watch set as part of a query, which can be used to
26  detect when a modification has been made to the database which affects the query
27  results. This lets callers easily watch for changes in the database in a very general
28  way.
29
30For the underlying immutable radix trees, see [go-immutable-radix](https://github.com/hashicorp/go-immutable-radix).
31
32Documentation
33=============
34
35The full documentation is available on [Godoc](https://pkg.go.dev/github.com/hashicorp/go-memdb).
36
37Example
38=======
39
40Below is a [simple example](https://play.golang.org/p/gCGE9FA4og1) of usage
41
42```go
43// Create a sample struct
44type Person struct {
45	Email string
46	Name  string
47	Age   int
48}
49
50// Create the DB schema
51schema := &memdb.DBSchema{
52	Tables: map[string]*memdb.TableSchema{
53		"person": &memdb.TableSchema{
54			Name: "person",
55			Indexes: map[string]*memdb.IndexSchema{
56				"id": &memdb.IndexSchema{
57					Name:    "id",
58					Unique:  true,
59					Indexer: &memdb.StringFieldIndex{Field: "Email"},
60				},
61				"age": &memdb.IndexSchema{
62					Name:    "age",
63					Unique:  false,
64					Indexer: &memdb.IntFieldIndex{Field: "Age"},
65				},
66			},
67		},
68	},
69}
70
71// Create a new data base
72db, err := memdb.NewMemDB(schema)
73if err != nil {
74	panic(err)
75}
76
77// Create a write transaction
78txn := db.Txn(true)
79
80// Insert some people
81people := []*Person{
82	&Person{"joe@aol.com", "Joe", 30},
83	&Person{"lucy@aol.com", "Lucy", 35},
84	&Person{"tariq@aol.com", "Tariq", 21},
85	&Person{"dorothy@aol.com", "Dorothy", 53},
86}
87for _, p := range people {
88	if err := txn.Insert("person", p); err != nil {
89		panic(err)
90	}
91}
92
93// Commit the transaction
94txn.Commit()
95
96// Create read-only transaction
97txn = db.Txn(false)
98defer txn.Abort()
99
100// Lookup by email
101raw, err := txn.First("person", "id", "joe@aol.com")
102if err != nil {
103	panic(err)
104}
105
106// Say hi!
107fmt.Printf("Hello %s!\n", raw.(*Person).Name)
108
109// List all the people
110it, err := txn.Get("person", "id")
111if err != nil {
112	panic(err)
113}
114
115fmt.Println("All the people:")
116for obj := it.Next(); obj != nil; obj = it.Next() {
117	p := obj.(*Person)
118	fmt.Printf("  %s\n", p.Name)
119}
120
121// Range scan over people with ages between 25 and 35 inclusive
122it, err = txn.LowerBound("person", "age", 25)
123if err != nil {
124	panic(err)
125}
126
127fmt.Println("People aged 25 - 35:")
128for obj := it.Next(); obj != nil; obj = it.Next() {
129	p := obj.(*Person)
130	if p.Age > 35 {
131		break
132	}
133	fmt.Printf("  %s is aged %d\n", p.Name, p.Age)
134}
135// Output:
136// Hello Joe!
137// All the people:
138//   Dorothy
139//   Joe
140//   Lucy
141//   Tariq
142// People aged 25 - 35:
143//   Joe is aged 30
144//   Lucy is aged 35
145```
146
147