db.go 2.91 KB
Newer Older
cznic's avatar
cznic committed
1
2
3
4
// Copyright 2015 The XC Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

cznic's avatar
cznic committed
5
package xc // import "modernc.org/xc"
cznic's avatar
cznic committed
6
7
8
9
10

import (
	"encoding/binary"
	"sync"

cznic's avatar
cznic committed
11
	"modernc.org/mathutil"
cznic's avatar
cznic committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
)

const (
	maxUvarint  = (64 + 7) / 7
	dbPageShift = 20 //TODO bench tune.
	dbPageSize  = 1 << dbPageShift
	dbPageMask  = dbPageSize - 1
)

// MemDB stores data accessible using a numeric identifier.
type MemDB struct {
	b256   [256]byte
	mu     sync.RWMutex // Access guard.
	nextID int
	pages  [][]byte
}

cznic's avatar
cznic committed
29
30
// NewMemDB returns a newly create *MemDB.
func NewMemDB() *MemDB {
cznic's avatar
cznic committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
	d := &MemDB{}
	for i := 0; i < 256; i++ {
		d.b256[i] = byte(i)
	}
	return d
}

// Bytes returns the byte slice associated with id. Passing id not obtained by
// PutBytes has undefined behavior. Bytes RLocks the DB to obtain the result.
//
// NOTE: The result refers to the DB backing storage directly without copying
// and is thus strictly R/O.
func (d *MemDB) Bytes(id int) []byte {
	if id == 0 {
		return nil
	}

	if id < 257 {
cznic's avatar
cznic committed
49
		return d.b256[id-1 : id : id]
cznic's avatar
cznic committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	}

	id -= 257
	off := id & dbPageMask

	d.mu.RLock() // R+

	page := d.pages[id>>dbPageShift]
	n, l := binary.Uvarint(page[off:])
	off += l
	h := off + int(n)
	r := page[off:h:h]

	d.mu.RUnlock() // R-

	return r
}

// bytesUnlocked is like Bytes but it does not RLock the DB.
func (d *MemDB) bytesUnlocked(id int) []byte {
	if id == 0 {
		return nil
	}

	if id < 257 {
cznic's avatar
cznic committed
75
		return d.b256[id-1 : id : id]
cznic's avatar
cznic committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	}

	id -= 257
	off := id & dbPageMask

	page := d.pages[id>>dbPageShift]
	n, l := binary.Uvarint(page[off:])
	off += l
	r := page[off : off+int(n)]

	return r
}

// Cap reports the current DB capacity.
func (d *MemDB) Cap() int {
	d.mu.RLock() // R+

	r := 0
	for _, v := range d.pages {
		r += cap(v)
	}

	d.mu.RUnlock() // R-

	return r
}

// Len reports the size of the DB used by data.
func (d *MemDB) Len() int {
	d.mu.RLock() // R+

	r := 0
	for _, v := range d.pages {
		r += len(v)
	}

	d.mu.RUnlock() // R-

	return r
}

// PutBytes stores b in the DB and returns its id.  Zero length byte slices are
// guaranteed to return zero ID. PutBytes Locks the DB before updating it.
func (d *MemDB) PutBytes(b []byte) int {
	if len(b) == 0 {
		return 0
	}

	if len(b) == 1 {
		return int(b[0]) + 1
	}

	d.mu.Lock() // W+

	id := d.nextID
	pi := id >> dbPageShift
	if pi < len(d.pages) {
		p := d.pages[pi]
		off := id & dbPageMask
		if n := cap(p) - off - maxUvarint; n >= len(b) {
			p = p[:cap(p)]
			l := binary.PutUvarint(p[off:], uint64(len(b)))
			copy(p[off+l:], b)
			n = l + len(b)
			d.pages[pi] = p[:off+n]
			d.nextID += n

			d.mu.Unlock() // W-

			return id + 257
		}

		pi++
	}

	p := make([]byte, mathutil.Max(dbPageSize, maxUvarint+len(b)))
	p = p[:binary.PutUvarint(p, uint64(len(b)))]
	p = append(p, b...)
	d.pages = append(d.pages, p)
	id = pi << dbPageShift
	d.nextID = id + mathutil.Min(dbPageSize, len(p))

	d.mu.Unlock() // W-

	return id + 257
}