1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

feat(cache): implement Delete

This commit is contained in:
wdvxdr 2021-10-08 21:41:47 +08:00
parent cd5c6c6a72
commit 0b04ec9adb
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
3 changed files with 43 additions and 7 deletions

View File

@ -498,6 +498,27 @@ func (d *DB) Get(hash *byte) []byte {
} }
// Delete remove item with the given key 'hash' from the database file. // Delete remove item with the given key 'hash' from the database file.
func (d *DB) Delete(sha1 *byte) error { func (d *DB) Delete(hash *byte) error {
return errors.New("impl me") var h [hashSize]byte
copyhash(&h[0], hash)
off := d.delete(d.top, &h[0])
if off == 0 {
return nil // not found key
}
d.top = collapse(d, d.top)
freeQueued(d)
d.flushSuper()
d.fd.Seek(off, io.SeekStart)
length, err := read32(d.fd) // len: 0
if err != nil {
return errors.Wrap(err, "btree I/O error")
}
d.freeChunk(off, int(length+4))
freeQueued(d)
d.flushSuper()
return nil
} }

View File

@ -9,7 +9,7 @@ import (
) )
func tempfile(t *testing.T) string { func tempfile(t *testing.T) string {
temp, err := os.CreateTemp("", "temp.*.db") temp, err := os.CreateTemp(".", "temp.*.db")
assert2.NoError(t, temp.Close()) assert2.NoError(t, temp.Close())
assert2.NoError(t, err) assert2.NoError(t, err)
return temp.Name() return temp.Name()
@ -48,9 +48,14 @@ func TestBtree(t *testing.T) {
for i, tt := range tests { for i, tt := range tests {
assert2.Equal(t, []byte(tt), bt.Get(sha[i])) assert2.Equal(t, []byte(tt), bt.Get(sha[i]))
} }
for i := range tests {
assert2.NoError(t, bt.Delete(sha[i]))
}
for i := range tests {
assert2.Equal(t, []byte(nil), bt.Get(sha[i]))
}
assert2.NoError(t, bt.Close()) assert2.NoError(t, bt.Close())
} }
func TestOpen(t *testing.T) {
println(tableSize)
}

View File

@ -50,6 +50,16 @@ func (c *Cache) Get(md5 []byte) []byte {
return c.db.Get(&hash[0]) return c.db.Get(&hash[0])
} }
// Delete 删除指定缓存
func (c *Cache) Delete(md5 []byte) {
c.lock.Lock()
defer c.lock.Unlock()
var hash [16]byte
copy(hash[:], md5)
_ = c.db.Delete(&hash[0])
}
// Init 初始化 Cache // Init 初始化 Cache
func Init() { func Init() {
node, ok := base.Database["cache"] node, ok := base.Database["cache"]