Debian Codegraph @

lmdb/val.go

raw symbols SCIP
1 package lmdb
2
3 /*
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include "lmdb.h"
7 */
8 import "C"
9
10 import (
11 "unsafe"
12
13 "github.com/bmatsuo/lmdb-go/internal/lmdbarch"
14 )
15
16 // valSizeBits is the number of bits which constraining the length of the
17 // single values in an LMDB database, either 32 or 31 depending on the
18 // platform. valMaxSize is the largest data size allowed based. See runtime
19 // source file malloc.go and the compiler typecheck.go for more information
20 // about memory limits and array bound limits.
21 //
22 // https://github.com/golang/go/blob/a03bdc3e6bea34abd5077205371e6fb9ef354481/src/runtime/malloc.go#L151-L164
23 // https://github.com/golang/go/blob/36a80c5941ec36d9c44d6f3c068d13201e023b5f/src/cmd/compile/internal/gc/typecheck.go#L383
24 //
25 // On 64-bit systems, luckily, the value 2^32-1 coincides with the maximum data
26 // size for LMDB (MAXDATASIZE).
27 const (
28 valSizeBits = lmdbarch.Width64*32 + (1-lmdbarch.Width64)*31
29 valMaxSize = 1<<valSizeBits - 1
30 )
31
32 // Multi is a wrapper for a contiguous page of sorted, fixed-length values
33 // passed to Cursor.PutMulti or retrieved using Cursor.Get with the
34 // GetMultiple/NextMultiple flag.
35 //
36 // Multi values are only useful in databases opened with DupSort|DupFixed.
37 type Multi struct {
38 page []byte
39 stride int
40 }
41
42 // WrapMulti converts a page of contiguous values with stride size into a
43 // Multi. WrapMulti panics if len(page) is not a multiple of stride.
44 //
45 // _, val, _ := cursor.Get(nil, nil, lmdb.FirstDup)
46 // _, page, _ := cursor.Get(nil, nil, lmdb.GetMultiple)
47 // multi := lmdb.WrapMulti(page, len(val))
48 //
49 // See mdb_cursor_get and MDB_GET_MULTIPLE.
50 func WrapMulti(page []byte, stride int) *Multi {
51 if len(page)%stride != 0 {
52 panic("incongruent arguments")
53 }
54 return &Multi{page: page, stride: stride}
55 }
56
57 // Vals returns a slice containing the values in m. The returned slice has
58 // length m.Len() and each item has length m.Stride().
59 func (m *Multi) Vals() [][]byte {
60 n := m.Len()
61 ps := make([][]byte, n)
62 for i := 0; i < n; i++ {
63 ps[i] = m.Val(i)
64 }
65 return ps
66 }
67
68 // Val returns the value at index i. Val panics if i is out of range.
69 func (m *Multi) Val(i int) []byte {
70 off := i * m.stride
71 return m.page[off : off+m.stride]
72 }
73
74 // Len returns the number of values in the Multi.
75 func (m *Multi) Len() int {
76 return len(m.page) / m.stride
77 }
78
79 // Stride returns the length of an individual value in the m.
80 func (m *Multi) Stride() int {
81 return m.stride
82 }
83
84 // Size returns the total size of the Multi data and is equal to
85 //
86 // m.Len()*m.Stride()
87 //
88 func (m *Multi) Size() int {
89 return len(m.page)
90 }
91
92 // Page returns the Multi page data as a raw slice of bytes with length
93 // m.Size().
94 func (m *Multi) Page() []byte {
95 return m.page[:len(m.page):len(m.page)]
96 }
97
98 var eb = []byte{0}
99
100 func valBytes(b []byte) ([]byte, int) {
101 if len(b) == 0 {
102 return eb, 0
103 }
104 return b, len(b)
105 }
106
107 func wrapVal(b []byte) *C.MDB_val {
108 p, n := valBytes(b)
109 return &C.MDB_val{
110 mv_data: unsafe.Pointer(&p[0]),
111 mv_size: C.size_t(n),
112 }
113 }
114
115 func getBytes(val *C.MDB_val) []byte {
116 return (*[valMaxSize]byte)(unsafe.Pointer(val.mv_data))[:val.mv_size:val.mv_size]
117 }
118
119 func getBytesCopy(val *C.MDB_val) []byte {
120 return C.GoBytes(val.mv_data, C.int(val.mv_size))
121 }