Re-use zstd encoder
This commit is contained in:
parent
2f06ae3605
commit
6bd97f30c2
|
@ -21,6 +21,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/klauspost/compress/zstd"
|
||||||
"seedno.de/seednode/roulette/types"
|
"seedno.de/seednode/roulette/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -447,18 +448,18 @@ func scanPaths(paths []string, sort string, index *fileIndex, formats types.Type
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileList(paths []string, filters *filters, sort string, index *fileIndex, formats types.Types, errorChannel chan<- error) []string {
|
func fileList(paths []string, filters *filters, sort string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) []string {
|
||||||
switch {
|
switch {
|
||||||
case Index && !index.isEmpty() && filters.isEmpty():
|
case Index && !index.isEmpty() && filters.isEmpty():
|
||||||
return index.List()
|
return index.List()
|
||||||
case Index && !index.isEmpty() && !filters.isEmpty():
|
case Index && !index.isEmpty() && !filters.isEmpty():
|
||||||
return filters.apply(index.List())
|
return filters.apply(index.List())
|
||||||
case Index && index.isEmpty() && !filters.isEmpty():
|
case Index && index.isEmpty() && !filters.isEmpty():
|
||||||
index.set(scanPaths(paths, sort, index, formats, errorChannel), errorChannel)
|
index.set(scanPaths(paths, sort, index, formats, errorChannel), encoder, errorChannel)
|
||||||
|
|
||||||
return filters.apply(index.List())
|
return filters.apply(index.List())
|
||||||
case Index && index.isEmpty() && filters.isEmpty():
|
case Index && index.isEmpty() && filters.isEmpty():
|
||||||
index.set(scanPaths(paths, sort, index, formats, errorChannel), errorChannel)
|
index.set(scanPaths(paths, sort, index, formats, errorChannel), encoder, errorChannel)
|
||||||
|
|
||||||
return index.List()
|
return index.List()
|
||||||
case !Index && !filters.isEmpty():
|
case !Index && !filters.isEmpty():
|
||||||
|
|
38
cmd/index.go
38
cmd/index.go
|
@ -7,7 +7,6 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -62,7 +61,7 @@ func (index *fileIndex) remove(path string) {
|
||||||
index.mutex.Unlock()
|
index.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (index *fileIndex) set(val []string, errorChannel chan<- error) {
|
func (index *fileIndex) set(val []string, encoder *zstd.Encoder, errorChannel chan<- error) {
|
||||||
length := len(val)
|
length := len(val)
|
||||||
|
|
||||||
if length < 1 {
|
if length < 1 {
|
||||||
|
@ -75,7 +74,7 @@ func (index *fileIndex) set(val []string, errorChannel chan<- error) {
|
||||||
index.mutex.Unlock()
|
index.mutex.Unlock()
|
||||||
|
|
||||||
if Index && IndexFile != "" {
|
if Index && IndexFile != "" {
|
||||||
index.Export(IndexFile, errorChannel)
|
index.Export(IndexFile, encoder, errorChannel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,20 +92,7 @@ func (index *fileIndex) isEmpty() bool {
|
||||||
return length == 0
|
return length == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func getReader(file io.Reader) (*zstd.Decoder, error) {
|
func (index *fileIndex) Export(path string, encoder *zstd.Encoder, errorChannel chan<- error) {
|
||||||
reader, err := zstd.NewReader(file)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return reader, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getWriter(file io.WriteCloser) (*zstd.Encoder, error) {
|
|
||||||
return zstd.NewWriter(file, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (index *fileIndex) Export(path string, errorChannel chan<- error) {
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||||
|
@ -117,14 +103,8 @@ func (index *fileIndex) Export(path string, errorChannel chan<- error) {
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
encoder, err := getWriter(file)
|
encoder.Reset(file)
|
||||||
if err != nil {
|
|
||||||
errorChannel <- err
|
|
||||||
|
|
||||||
encoder.Close()
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer encoder.Close()
|
defer encoder.Close()
|
||||||
|
|
||||||
enc := gob.NewEncoder(encoder)
|
enc := gob.NewEncoder(encoder)
|
||||||
|
@ -181,7 +161,7 @@ func (index *fileIndex) Import(path string, errorChannel chan<- error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
reader, err := getReader(file)
|
reader, err := zstd.NewReader(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChannel <- err
|
errorChannel <- err
|
||||||
|
|
||||||
|
@ -214,13 +194,13 @@ func (index *fileIndex) Import(path string, errorChannel chan<- error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
|
func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) httprouter.Handle {
|
||||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
index.clear()
|
index.clear()
|
||||||
|
|
||||||
fileList(args, &filters{}, "", index, formats, errorChannel)
|
fileList(args, &filters{}, "", index, formats, encoder, errorChannel)
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
|
||||||
|
@ -241,10 +221,10 @@ func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func importIndex(args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) {
|
func importIndex(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) {
|
||||||
if IndexFile != "" {
|
if IndexFile != "" {
|
||||||
index.Import(IndexFile, errorChannel)
|
index.Import(IndexFile, errorChannel)
|
||||||
}
|
}
|
||||||
|
|
||||||
fileList(args, &filters{}, "", index, formats, errorChannel)
|
fileList(args, &filters{}, "", index, formats, encoder, errorChannel)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AllowedCharacters string = `^[A-z0-9.\-_]+$`
|
AllowedCharacters string = `^[A-z0-9.\-_]+$`
|
||||||
ReleaseVersion string = "5.4.0"
|
ReleaseVersion string = "5.4.1"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
18
cmd/web.go
18
cmd/web.go
|
@ -21,6 +21,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"github.com/klauspost/compress/zstd"
|
||||||
"github.com/yosssi/gohtml"
|
"github.com/yosssi/gohtml"
|
||||||
"seedno.de/seednode/roulette/types"
|
"seedno.de/seednode/roulette/types"
|
||||||
"seedno.de/seednode/roulette/types/audio"
|
"seedno.de/seednode/roulette/types/audio"
|
||||||
|
@ -31,6 +32,8 @@ import (
|
||||||
"seedno.de/seednode/roulette/types/video"
|
"seedno.de/seednode/roulette/types/video"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ()
|
||||||
|
|
||||||
const (
|
const (
|
||||||
logDate string = `2006-01-02T15:04:05.000-07:00`
|
logDate string = `2006-01-02T15:04:05.000-07:00`
|
||||||
sourcePrefix string = `/source`
|
sourcePrefix string = `/source`
|
||||||
|
@ -168,7 +171,7 @@ func serveStaticFile(paths []string, index *fileIndex, errorChannel chan<- error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveRoot(paths []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
|
func serveRoot(paths []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) httprouter.Handle {
|
||||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||||
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -203,7 +206,7 @@ func serveRoot(paths []string, index *fileIndex, formats types.Types, errorChann
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
list := fileList(paths, filters, sortOrder, index, formats, errorChannel)
|
list := fileList(paths, filters, sortOrder, index, formats, encoder, errorChannel)
|
||||||
|
|
||||||
loop:
|
loop:
|
||||||
for timeout := time.After(timeout); ; {
|
for timeout := time.After(timeout); ; {
|
||||||
|
@ -576,7 +579,12 @@ func ServePage(args []string) error {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
registerHandler(mux, Prefix, serveRoot(paths, index, formats, errorChannel))
|
encoder, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
registerHandler(mux, Prefix, serveRoot(paths, index, formats, encoder, errorChannel))
|
||||||
|
|
||||||
Prefix = strings.TrimSuffix(Prefix, "/")
|
Prefix = strings.TrimSuffix(Prefix, "/")
|
||||||
|
|
||||||
|
@ -595,9 +603,9 @@ func ServePage(args []string) error {
|
||||||
registerHandler(mux, Prefix+"/version", serveVersion(errorChannel))
|
registerHandler(mux, Prefix+"/version", serveVersion(errorChannel))
|
||||||
|
|
||||||
if Index {
|
if Index {
|
||||||
registerHandler(mux, Prefix+AdminPrefix+"/index/rebuild", serveIndexRebuild(args, index, formats, errorChannel))
|
registerHandler(mux, Prefix+AdminPrefix+"/index/rebuild", serveIndexRebuild(args, index, formats, encoder, errorChannel))
|
||||||
|
|
||||||
importIndex(paths, index, formats, errorChannel)
|
importIndex(paths, index, formats, encoder, errorChannel)
|
||||||
}
|
}
|
||||||
|
|
||||||
if Info {
|
if Info {
|
||||||
|
|
Loading…
Reference in New Issue