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

fix: make linter happy in internal/btree

This commit is contained in:
wdvxdr 2021-09-28 10:48:19 +08:00
parent f767213681
commit 4625c785dd
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
2 changed files with 27 additions and 27 deletions

View File

@ -138,7 +138,7 @@ func Create(name string) (*Btree, error) {
// Close closes the database // Close closes the database
func (bt *Btree) Close() error { func (bt *Btree) Close() error {
err := bt.Close() err := bt.fd.Close()
for i := 0; i < cacheSlots; i++ { for i := 0; i < cacheSlots; i++ {
bt.cache[i] = cache{} bt.cache[i] = cache{}
} }
@ -271,7 +271,7 @@ func (bt *Btree) remove(t *table, i int, sha1 *byte) int64 {
return offset return offset
} }
func (bt *Btree) insert(toff int64, sha1 *byte, data []byte, len int) int64 { func (bt *Btree) insert(toff int64, sha1 *byte, data []byte, size int) int64 {
table := bt.get(toff) table := bt.get(toff)
assert(table.size < tableSize-1) assert(table.size < tableSize-1)
@ -296,7 +296,7 @@ func (bt *Btree) insert(toff int64, sha1 *byte, data []byte, len int) int64 {
lc := table.items[i].child lc := table.items[i].child
if lc != 0 { if lc != 0 {
/* recursion */ /* recursion */
ret = bt.insert(lc, sha1, data, len) ret = bt.insert(lc, sha1, data, size)
/* check if we need to split */ /* check if we need to split */
child := bt.get(lc) child := bt.get(lc)
@ -314,7 +314,7 @@ func (bt *Btree) insert(toff int64, sha1 *byte, data []byte, len int) int64 {
// make sure data is written before a reference is added to it // make sure data is written before a reference is added to it
_ = bt.fd.Sync() _ = bt.fd.Sync()
} else { } else {
off = bt.insertData(data, len) off = bt.insertData(data, size)
ret = off ret = off
} }
@ -399,10 +399,10 @@ func (bt *Btree) delete(offset int64, hash *byte) int64 {
return ret return ret
} }
func (bt *Btree) insertTopLevel(toff *int64, sha1 *byte, data []byte, len int) int64 { func (bt *Btree) insertTopLevel(toff *int64, sha1 *byte, data []byte, size int) int64 {
var off, ret, rc int64 var off, ret, rc int64
if *toff != 0 { if *toff != 0 {
ret = bt.insert(*toff, sha1, data, len) ret = bt.insert(*toff, sha1, data, size)
/* check if we need to split */ /* check if we need to split */
table := bt.get(*toff) table := bt.get(*toff)
@ -414,7 +414,7 @@ func (bt *Btree) insertTopLevel(toff *int64, sha1 *byte, data []byte, len int) i
rc = bt.split(table, sha1, &off) rc = bt.split(table, sha1, &off)
bt.flush(table, *toff) bt.flush(table, *toff)
} else { } else {
off = bt.insertData(data, len) off = bt.insertData(data, size)
ret = off ret = off
} }
@ -471,7 +471,7 @@ func (bt *Btree) Insert(csha1 *byte, data []byte) {
var sha1 [sha1Size]byte var sha1 [sha1Size]byte
copysha1(&sha1[0], csha1) copysha1(&sha1[0], csha1)
bt.insertTopLevel(&bt.top, &sha1[0], data, len(data)) _ = bt.insertTopLevel(&bt.top, &sha1[0], data, len(data))
freeQueued(bt) freeQueued(bt)
bt.flushSuper() bt.flushSuper()
} }

View File

@ -26,10 +26,10 @@ func freeQueued(bt *Btree) {
fqueueLen = 0 fqueueLen = 0
} }
func (bt *Btree) allocChunk(len int) int64 { func (bt *Btree) allocChunk(size int) int64 {
assert(len > 0) assert(size > 0)
len = power2(len) size = power2(size)
var offset int64 var offset int64
if bt.inAllocator { if bt.inAllocator {
@ -38,10 +38,10 @@ func (bt *Btree) allocChunk(len int) int64 {
/* create fake size SHA-1 */ /* create fake size SHA-1 */
var sha1 [sha1Size]byte var sha1 [sha1Size]byte
p := unsafe.Pointer(&sha1[0]) p := unsafe.Pointer(&sha1[0])
*(*int32)(p) = -1 // *(uint32_t *) sha1 = -1; *(*int32)(p) = -1 // *(uint32_t *) sha1 = -1;
*(*uint32)(unsafe.Add(p, i32s)) = uint32(len) // ((__be32 *) sha1)[1] = to_be32(len); *(*uint32)(unsafe.Add(p, i32s)) = uint32(size) // ((__be32 *) sha1)[1] = to_be32(size);
/* find free chunk with the larger or the same len/SHA-1 */ /* find free chunk with the larger or the same size/SHA-1 */
bt.inAllocator = true bt.inAllocator = true
bt.deleteLarger = true bt.deleteLarger = true
offset = bt.delete(bt.freeTop, &sha1[0]) offset = bt.delete(bt.freeTop, &sha1[0])
@ -50,20 +50,20 @@ func (bt *Btree) allocChunk(len int) int64 {
assert(*(*int32)(p) == -1) // assert(*(uint32_t *) sha1 == (uint32_t) -1) assert(*(*int32)(p) == -1) // assert(*(uint32_t *) sha1 == (uint32_t) -1)
flen := int(*(*uint32)(unsafe.Add(p, i32s))) // size_t free_len = from_be32(((__be32 *) sha1)[1]) flen := int(*(*uint32)(unsafe.Add(p, i32s))) // size_t free_len = from_be32(((__be32 *) sha1)[1])
assert(power2(flen) == flen) assert(power2(flen) == flen)
assert(flen >= len) assert(flen >= size)
/* delete buddy information */ /* delete buddy information */
resetsha1(&sha1[0]) resetsha1(&sha1[0])
*(*int64)(p) = offset *(*int64)(p) = offset
buddyLen := bt.delete(bt.freeTop, &sha1[0]) buddyLen := bt.delete(bt.freeTop, &sha1[0])
assert(buddyLen == int64(len)) assert(buddyLen == int64(size))
bt.freeTop = collapse(bt, bt.freeTop) bt.freeTop = collapse(bt, bt.freeTop)
bt.inAllocator = false bt.inAllocator = false
/* free extra space at the end of the chunk */ /* free extra space at the end of the chunk */
for flen > len { for flen > size {
flen >>= 1 flen >>= 1
bt.freeChunk(offset+int64(flen), flen) bt.freeChunk(offset+int64(flen), flen)
} }
@ -75,10 +75,10 @@ func (bt *Btree) allocChunk(len int) int64 {
/* not found, allocate from the end of the file */ /* not found, allocate from the end of the file */
offset = bt.alloc offset = bt.alloc
/* TODO: this wastes memory.. */ /* TODO: this wastes memory.. */
if offset&int64(len-1) != 0 { if offset&int64(size-1) != 0 {
offset += int64(len) - (offset & (int64(len) - 1)) offset += int64(size) - (offset & (int64(size) - 1))
} }
bt.alloc = offset + int64(len) bt.alloc = offset + int64(size)
} }
bt.flushSuper() bt.flushSuper()
@ -88,17 +88,17 @@ func (bt *Btree) allocChunk(len int) int64 {
} }
/* Mark a chunk as unused in the database file */ /* Mark a chunk as unused in the database file */
func (bt *Btree) freeChunk(offset int64, len int) { func (bt *Btree) freeChunk(offset int64, size int) {
assert(len > 0) assert(size > 0)
assert(offset != 0) assert(offset != 0)
len = power2(len) size = power2(size)
assert(offset&int64(len-1) == 0) assert(offset&int64(size-1) == 0)
if bt.inAllocator { if bt.inAllocator {
chunk := &fqueue[fqueueLen] chunk := &fqueue[fqueueLen]
fqueueLen++ fqueueLen++
chunk.offset = offset chunk.offset = offset
chunk.len = len chunk.len = size
return return
} }
@ -112,12 +112,12 @@ func (bt *Btree) freeChunk(offset int64, len int) {
/* add buddy information */ /* add buddy information */
resetsha1(&sha1[0]) resetsha1(&sha1[0])
*(*int32)(p) = -1 // *(uint32_t *) sha1 = -1; *(*int32)(p) = -1 // *(uint32_t *) sha1 = -1;
*(*uint32)(unsafe.Add(p, i32s)) = uint32(len) // ((__be32 *) sha1)[1] = to_be32(len); *(*uint32)(unsafe.Add(p, i32s)) = uint32(size) // ((__be32 *) sha1)[1] = to_be32(size);
*(*uint32)(unsafe.Add(p, i32s*2)) = rand.Uint32() /* to make SHA-1 unique */ *(*uint32)(unsafe.Add(p, i32s*2)) = rand.Uint32() /* to make SHA-1 unique */
*(*uint32)(unsafe.Add(p, i32s*3)) = rand.Uint32() *(*uint32)(unsafe.Add(p, i32s*3)) = rand.Uint32()
// insert_toplevel(btree, &btree->free_top, sha1, NULL, offset); // insert_toplevel(btree, &btree->free_top, sha1, NULL, offset);
bt.insertTopLevel(&bt.freeTop, &sha1[0], nil, int(offset)) _ = bt.insertTopLevel(&bt.freeTop, &sha1[0], nil, int(offset))
bt.inAllocator = false bt.inAllocator = false
bt.flushSuper() bt.flushSuper()