Key Value Store
There are cases where you don't want a separate server but embed immudb directly in the same application process, as a library.
immudb already provides an embeddable key-value store in the embedded package. The following example shows how to create or open a store, write some data and read it back.
go
package main
import (
"fmt"
"log"
"github.com/codenotary/immudb/embedded/store"
)
func handleErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
// create/open immudb store at specified path
st, err := store.Open("data", store.DefaultOptions())
handleErr(err)
// close the store to free resources
defer st.Close()
// create a transaction
tx, err := st.NewTx()
handleErr(err)
// ensure tx is closed (it won't affect committed tx)
defer tx.Cancel()
// write key-value pair into the tx context, no change will be applied yet
err = tx.Set([]byte("hello"), nil, []byte("immutable-world!"))
handleErr(err)
// transaction is committed and changes are applied
hdr, err := tx.Commit()
handleErr(err)
fmt.Printf("tx %d successfully committed\n", hdr.ID)
// fetch the latest entry of a key
// dsue to performance considerations, only metadata, hash, and size are returned at first
valRef, err := st.Get([]byte("hello"))
handleErr(err)
// read the actual value
val, err := valRef.Resolve()
handleErr(err)
fmt.Printf("key '%s' = '%s' found at tx %d (%d key-updates)\n", []byte("hello"), val, valRef.Tx(), valRef.HC())
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51