This commit is contained in:
2026-04-09 11:05:02 +08:00
parent a36c51de1e
commit 3d46c697a0
2 changed files with 23 additions and 9 deletions
+2 -2
View File
@@ -101,7 +101,7 @@ func GetDefaultConfig() Config {
Index: IndexConfig{ Index: IndexConfig{
MaxURLsPerKey: 11000, MaxURLsPerKey: 11000,
MaxSameDomainPerKey: 20, MaxSameDomainPerKey: 20,
BigCleanThreshold: 10000000, BigCleanThreshold: 2000000,
MaxNewURLsPerKey: 10000, MaxNewURLsPerKey: 10000,
MinURLsForNewKey: 3, MinURLsForNewKey: 3,
}, },
@@ -123,7 +123,7 @@ func GetDefaultConfig() Config {
ConsecutiveKeyWeight: 1.3, ConsecutiveKeyWeight: 1.3,
BacklinkWeight: 1.0, BacklinkWeight: 1.0,
ServerPort: 80, ServerPort: 80,
FlushIntervalSeconds: 60, FlushIntervalSeconds: 30,
}, },
Backlink: BacklinkConfig{ Backlink: BacklinkConfig{
Baseline: 200000, Baseline: 200000,
+21 -7
View File
@@ -90,7 +90,7 @@ func (d *DB) Close() error {
// brotli 压缩比高于 gzip,适合大量文本的存储空间优化。 // brotli 压缩比高于 gzip,适合大量文本的存储空间优化。
func compress(data []byte) ([]byte, error) { func compress(data []byte) ([]byte, error) {
buf := make([]byte, 0, len(data)) // 预分配,避免反复扩容 buf := make([]byte, 0, len(data)) // 预分配,避免反复扩容
w := brotli.NewWriterLevel((*appendWriter)(&buf), 6) // 压缩级别 6(平衡速度压缩比) w := brotli.NewWriterLevel((*appendWriter)(&buf), 3) // 压缩级别 3(优先速度压缩比损失约 10-15%
if _, err := w.Write(data); err != nil { if _, err := w.Write(data); err != nil {
return nil, err return nil, err
} }
@@ -210,14 +210,28 @@ func (d *DB) BatchSetIndex(batch map[string][]IndexEntry) error {
end = len(items) end = len(items)
} }
batchNum := i/batchSize + 1 batchNum := i/batchSize + 1
// 事务外预先完成所有序列化和压缩,减少事务持锁时间
preItems := make([]struct {
keyword string
data []byte
}, 0, end-i)
for _, item := range items[i:end] {
data, err := marshalCompress(item.entries)
if err != nil {
return err
}
preItems = append(preItems, struct {
keyword string
data []byte
}{item.keyword, data})
}
// 事务内只做纯内存写入,持锁时间极短
if err := d.db.Update(func(tx *bolt.Tx) error { if err := d.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketIndex) b := tx.Bucket(bucketIndex)
for _, item := range items[i:end] { for _, p := range preItems {
data, err := marshalCompress(item.entries) if err := b.Put([]byte(p.keyword), p.data); err != nil {
if err != nil {
return err
}
if err := b.Put([]byte(item.keyword), data); err != nil {
return err return err
} }
} }