forked from linxGnu/grocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
85 lines (69 loc) · 2.42 KB
/
cache.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package grocksdb
// #include "rocksdb/c.h"
import "C"
// Cache is a cache used to store data read from data in memory.
type Cache struct {
c *C.rocksdb_cache_t
}
// NewLRUCache creates a new LRU Cache object with the capacity given.
func NewLRUCache(capacity uint64) *Cache {
return NewNativeCache(C.rocksdb_cache_create_lru(C.size_t(capacity)))
}
// NewLRUCacheWithOptions creates a new LRU Cache from options.
func NewLRUCacheWithOptions(opt *LRUCacheOptions) *Cache {
return NewNativeCache(C.rocksdb_cache_create_lru_opts(opt.c))
}
// NewNativeCache creates a Cache object.
func NewNativeCache(c *C.rocksdb_cache_t) *Cache {
return &Cache{c}
}
// GetUsage returns the Cache memory usage.
func (c *Cache) GetUsage() uint64 {
return uint64(C.rocksdb_cache_get_usage(c.c))
}
// GetPinnedUsage returns the Cache pinned memory usage.
func (c *Cache) GetPinnedUsage() uint64 {
return uint64(C.rocksdb_cache_get_pinned_usage(c.c))
}
// SetCapacity sets capacity of the cache.
func (c *Cache) SetCapacity(value uint64) {
C.rocksdb_cache_set_capacity(c.c, C.size_t(value))
}
// GetCapacity returns capacity of the cache.
func (c *Cache) GetCapacity() uint64 {
return uint64(C.rocksdb_cache_get_capacity(c.c))
}
// Disowndata call this on shutdown if you want to speed it up. Cache will disown
// any underlying data and will not free it on delete. This call will leak
// memory - call this only if you're shutting down the process.
// Any attempts of using cache after this call will fail terribly.
// Always delete the DB object before calling this method!
func (c *Cache) DisownData() {
C.rocksdb_cache_disown_data(c.c)
}
// Destroy deallocates the Cache object.
func (c *Cache) Destroy() {
C.rocksdb_cache_destroy(c.c)
c.c = nil
}
// LRUCacheOptions are options for LRU Cache.
type LRUCacheOptions struct {
c *C.rocksdb_lru_cache_options_t
}
// NewLRUCacheOptions creates lru cache options.
func NewLRUCacheOptions() *LRUCacheOptions {
return &LRUCacheOptions{c: C.rocksdb_lru_cache_options_create()}
}
// Destroy lru cache options.
func (l *LRUCacheOptions) Destroy() {
C.rocksdb_lru_cache_options_destroy(l.c)
l.c = nil
}
// SetCapacity sets capacity for this lru cache.
func (l *LRUCacheOptions) SetCapacity(s uint) {
C.rocksdb_lru_cache_options_set_capacity(l.c, C.ulong(s))
}
// SetMemoryAllocator for this lru cache.
func (l *LRUCacheOptions) SetMemoryAllocator(m *MemoryAllocator) {
C.rocksdb_lru_cache_options_set_memory_allocator(l.c, m.c)
}