-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindexdb.go
63 lines (52 loc) · 1.56 KB
/
indexdb.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
// Copyright 2016 The clang-server Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package indexdb
import (
"path/filepath"
"github.com/syndtr/goleveldb/leveldb"
"github.com/zchee/clang-server/internal/pathutil"
)
// index directory name of leveldb which indexed C/C++ symbols.
const index = "index.db"
// IndexDB represets a C/C++ file database using leveldb.
type IndexDB struct {
root string
ldb *leveldb.DB
}
// NewIndexDB creates the project cache directory, open the leveldb db file
// on same location and return the new IndexDB.
func NewIndexDB(root string) (*IndexDB, error) {
dir := pathutil.ProjectCacheDir(root)
db, err := leveldb.OpenFile(filepath.Join(dir, index), nil)
if err != nil {
return nil, err
}
return &IndexDB{
root: root,
ldb: db,
}, nil
}
// Close closes the leveldb.
func (i *IndexDB) Close() error {
return i.ldb.Close()
}
// Put puts the selialized C/C++ files symbol data to leveldb.
// The key is using blake2b hashed filename.
func (i *IndexDB) Put(key []byte, value []byte) error {
return i.ldb.Put(key[:], value, nil)
}
// Get gets the selialized C/C++ files symbol data from leveldb.
// The key is using blake2b hashed filename.
func (i *IndexDB) Get(key []byte) ([]byte, error) {
return i.ldb.Get(key[:], nil)
}
// Has reports whether filename symbol data on leveldb.
// The key is using blake2b hashed filename.
func (i *IndexDB) Has(key []byte) bool {
has, err := i.ldb.Has(key[:], nil)
if err != nil {
return false
}
return has
}