README.md
1# go-memdb
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](http://godoc.org/github.com/hashicorp/go-memdb).
36
37Example
38=======
39
40Below is a simple example 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 },
62 },
63 },
64}
65
66// Create a new data base
67db, err := memdb.NewMemDB(schema)
68if err != nil {
69 panic(err)
70}
71
72// Create a write transaction
73txn := db.Txn(true)
74
75// Insert a new person
76p := &Person{"joe@aol.com", "Joe", 30}
77if err := txn.Insert("person", p); err != nil {
78 panic(err)
79}
80
81// Commit the transaction
82txn.Commit()
83
84// Create read-only transaction
85txn = db.Txn(false)
86defer txn.Abort()
87
88// Lookup by email
89raw, err := txn.First("person", "id", "joe@aol.com")
90if err != nil {
91 panic(err)
92}
93
94// Say hi!
95fmt.Printf("Hello %s!", raw.(*Person).Name)
96
97```
98
99