-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdb.go
319 lines (279 loc) · 8.91 KB
/
db.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package notmuch
// Copyright © 2015 The go.notmuch Authors. Authors can be found in the AUTHORS file.
// Licensed under the GPLv3 or later.
// See COPYING at the root of the repository for details.
// #cgo LDFLAGS: -lnotmuch
// #include <stdlib.h>
// #include <notmuch.h>
import "C"
import (
"unsafe"
)
const (
// DBReadOnly is the mode for opening the database in read only.
DBReadOnly = C.NOTMUCH_DATABASE_MODE_READ_ONLY
// DBReadWrite is the mode for opening the database in read write.
DBReadWrite = C.NOTMUCH_DATABASE_MODE_READ_WRITE
// TagMax is the maximum number of allowed tags.
TagMax = C.NOTMUCH_TAG_MAX
)
type (
// DBMode is the mode of the database opening, DBReadOnly or DBReadWrite
DBMode C.notmuch_database_mode_t
// DB represents a notmuch database.
DB cStruct
)
func (db *DB) toC() *C.notmuch_database_t {
return (*C.notmuch_database_t)(db.cptr)
}
// Close closes the database.
func (db *DB) Close() error {
return (*cStruct)(db).doClose(func() error {
return statusErr(C.notmuch_database_destroy(db.toC()))
})
}
// Create creates a new, empty notmuch database located at 'path'.
func Create(path string) (*DB, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var cdb *C.notmuch_database_t
err := statusErr(C.notmuch_database_create(cpath, &cdb))
if err != nil {
return nil, err
}
db := &DB{cptr: unsafe.Pointer(cdb)}
setGcClose(db)
return db, nil
}
// Open opens the database at the location path using mode. Caller is responsible
// for closing the database when done.
func Open(path string, mode DBMode) (*DB, error) {
config := ""
return OpenWithConfig(&path, &config, nil, mode)
}
// OpenWithConfig opens the database at the location 'path' using 'mode' and
// the configuration in 'config'.
//
// If 'path' is nil, use the location specified:
// - in the environment variable $NOTMUCH_DATABASE, if non-empty
// - in a configuration file, located as described in 'config'
// - by $XDG_DATA_HOME/notmuch/<profile>, if profile argument is set
//
// If 'path' is non-nil, but does not appear to be a Xapian database, check
// for a directory '.notmuch/xapian' below 'path'.
//
// If 'config' is nil, it will look:
// - the environment variable $NOTMUCH_CONFIG, if non-empty
// - $XDG_CONFIG_HOME/notmuch
// - $HOME/.notmuch-config
//
// If 'config' is an empty string (""), then it will not open any configuration
// file.
//
// If 'profile' is nil, it will use:
// - the environment variable $NOTMUCH_PROFILE if defined
// - otherwise 'default' for directories, and '' for paths
//
// If 'profile' is non-nil, append to the directory / file path determined
// for 'config' and 'path'.
//
// Caller is responsible for closing the database when done.
func OpenWithConfig(path, config, profile *string, mode DBMode) (*DB, error) {
var cpath *C.char
if path != nil {
cpath = C.CString(*path)
defer C.free(unsafe.Pointer(cpath))
}
var cconfig *C.char
if config != nil {
cconfig = C.CString(*config)
defer C.free(unsafe.Pointer(cconfig))
}
var cprofile *C.char
if profile != nil {
cprofile = C.CString(*profile)
defer C.free(unsafe.Pointer(cprofile))
}
var errMsg string
cErrMsg := C.CString(errMsg)
defer C.free(unsafe.Pointer(cErrMsg))
cmode := C.notmuch_database_mode_t(mode)
var cdb *C.notmuch_database_t
cdbptr := (**C.notmuch_database_t)(&cdb)
err := statusErr(C.notmuch_database_open_with_config(cpath, cmode, cconfig, cprofile, cdbptr, &cErrMsg))
if err != nil || errMsg != "" {
return nil, err
}
db := &DB{cptr: unsafe.Pointer(cdb)}
setGcClose(db)
return db, nil
}
// Compact compacts a notmuch database, backing up the original database to the
// given path. The database will be opened with DBReadWrite to ensure no writes
// are made.
func Compact(path, backup string) error {
cpath := C.CString(path)
cbackup := C.CString(backup)
defer func() {
C.free(unsafe.Pointer(cpath))
C.free(unsafe.Pointer(cbackup))
}()
return statusErr(C.notmuch_database_compact(cpath, cbackup, nil, nil))
}
// Atomic opens an atomic transaction in the database and calls the callback.
func (db *DB) Atomic(callback func(*DB)) error {
if err := statusErr(C.notmuch_database_begin_atomic(db.toC())); err != nil {
return err
}
callback(db)
return statusErr(C.notmuch_database_end_atomic(db.toC()))
}
// NewQuery creates a new query from a string following xapian format.
func (db *DB) NewQuery(queryString string) *Query {
cstr := C.CString(queryString)
defer C.free(unsafe.Pointer(cstr))
cquery := C.notmuch_query_create(db.toC(), cstr)
query := &Query{
cptr: unsafe.Pointer(cquery),
parent: (*cStruct)(db),
}
setGcClose(query)
return query
}
// Version returns the database version.
func (db *DB) Version() int {
return int(C.notmuch_database_get_version(db.toC()))
}
// LastStatus retrieves last status string for the notmuch database.
func (db *DB) LastStatus() string {
return C.GoString(C.notmuch_database_status_string(db.toC()))
}
// Path returns the database path of the database.
func (db *DB) Path() string {
return C.GoString(C.notmuch_database_get_path(db.toC()))
}
// NeedsUpgrade returns true if the database can be upgraded. This will always
// return false if the database was opened with DBReadOnly.
//
// If this function returns true then the caller may call
// Upgrade() to upgrade the database.
func (db *DB) NeedsUpgrade() bool {
cbool := C.notmuch_database_needs_upgrade(db.toC())
return int(cbool) != 0
}
// Upgrade upgrades the current database to the latest supported version. The
// database must be opened with DBReadWrite.
func (db *DB) Upgrade() error {
return statusErr(C.notmuch_database_upgrade(db.toC(), nil, nil))
}
// AddMessage adds a new message to the current database or associate an
// additional filename with an existing message.
func (db *DB) AddMessage(filename string) (*Message, error) {
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
var cmsg *C.notmuch_message_t
err := statusErr(C.notmuch_database_index_file(db.toC(), cfilename, nil, &cmsg))
if err != nil && err != ErrDuplicateMessageID {
return nil, err
}
msg := &Message{
cptr: unsafe.Pointer(cmsg),
parent: (*cStruct)(db),
}
setGcClose(msg)
return msg, err
}
// RemoveMessage remove a message filename from the current database. If the
// message has no more filenames, remove the message.
func (db *DB) RemoveMessage(filename string) error {
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
return statusErr(C.notmuch_database_remove_message(db.toC(), cfilename))
}
// FindMessage finds a message with the given message_id.
func (db *DB) FindMessage(id string) (*Message, error) {
cid := C.CString(id)
defer C.free(unsafe.Pointer(cid))
var cmsg *C.notmuch_message_t
if err := statusErr(C.notmuch_database_find_message(db.toC(), cid, &cmsg)); err != nil {
return nil, err
}
if cmsg == nil {
return nil, ErrNotFound
}
msg := &Message{
cptr: unsafe.Pointer(cmsg),
parent: (*cStruct)(db),
}
setGcClose(msg)
return msg, nil
}
// FindMessageByFilename finds a message with the given filename.
func (db *DB) FindMessageByFilename(filename string) (*Message, error) {
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
var cmsg *C.notmuch_message_t
if err := statusErr(C.notmuch_database_find_message_by_filename(db.toC(), cfilename, &cmsg)); err != nil {
return nil, err
}
if cmsg == nil {
return nil, ErrNotFound
}
msg := &Message{
cptr: unsafe.Pointer(cmsg),
parent: (*cStruct)(db),
}
setGcClose(msg)
return msg, nil
}
// Tags returns the list of all tags in the database.
func (db *DB) Tags() (*Tags, error) {
ctags := C.notmuch_database_get_all_tags(db.toC())
if ctags == nil {
return nil, ErrUnknownError
}
tags := &Tags{
cptr: unsafe.Pointer(ctags),
parent: (*cStruct)(db),
}
setGcClose(tags)
return tags, nil
}
// GetConfigList returns the config list, which can be used to iterate over all
// set options starting with prefix.
func (db *DB) GetConfigList(prefix string) (*ConfigList, error) {
cstr := C.CString(prefix)
defer C.free(unsafe.Pointer(cstr))
var ccl *C.notmuch_config_list_t
cclptr := (**C.notmuch_config_list_t)(&ccl)
err := statusErr(C.notmuch_database_get_config_list(db.toC(), cstr, cclptr))
if err != nil {
return nil, err
}
cl := &ConfigList{
cptr: unsafe.Pointer(ccl),
parent: (*cStruct)(db),
}
setGcClose(cl)
return cl, nil
}
// GetConfig gets config value of key
func (db *DB) GetConfig(key string) (string, error) {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
var cval *C.char
err := statusErr(C.notmuch_database_get_config(db.toC(), ckey, &cval))
if err != nil {
return "", err
}
defer C.free(unsafe.Pointer(cval))
return C.GoString(cval), nil
}
// SetConfig sets config key to value.
func (db *DB) SetConfig(key, value string) error {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
cval := C.CString(value)
defer C.free(unsafe.Pointer(cval))
return statusErr(C.notmuch_database_set_config(db.toC(), ckey, cval))
}