2018-01-31 02:15:21 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-06-15 04:30:37 +02:00
|
|
|
"context"
|
2018-01-31 02:15:21 +01:00
|
|
|
"database/sql"
|
2021-01-07 01:26:30 +01:00
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2020-12-30 04:24:11 +01:00
|
|
|
"strings"
|
2019-06-25 15:13:25 +02:00
|
|
|
"time"
|
2018-01-31 02:15:21 +01:00
|
|
|
|
2019-11-14 01:11:35 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/dht/bits"
|
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
qt "github.com/lbryio/lbry.go/v2/extras/query"
|
2021-01-07 01:26:30 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/stop"
|
2020-12-30 04:24:11 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
2018-08-07 22:51:02 +02:00
|
|
|
|
2019-06-26 20:36:05 +02:00
|
|
|
"github.com/go-sql-driver/mysql"
|
|
|
|
_ "github.com/go-sql-driver/mysql" // blank import for db driver ensures its imported even if its not used
|
2018-01-31 02:15:21 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-06 15:39:43 +02:00
|
|
|
"github.com/volatiletech/null"
|
2021-01-07 01:26:30 +01:00
|
|
|
"go.uber.org/atomic"
|
2018-01-31 02:15:21 +01:00
|
|
|
)
|
|
|
|
|
2018-08-07 22:51:02 +02:00
|
|
|
// SdBlob is a special blob that contains information on the rest of the blobs in the stream
|
|
|
|
type SdBlob struct {
|
|
|
|
StreamName string `json:"stream_name"`
|
|
|
|
Blobs []struct {
|
|
|
|
Length int `json:"length"`
|
|
|
|
BlobNum int `json:"blob_num"`
|
|
|
|
BlobHash string `json:"blob_hash,omitempty"`
|
2018-08-20 23:50:39 +02:00
|
|
|
IV string `json:"iv"`
|
2018-08-07 22:51:02 +02:00
|
|
|
} `json:"blobs"`
|
|
|
|
StreamType string `json:"stream_type"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
SuggestedFileName string `json:"suggested_file_name"`
|
|
|
|
StreamHash string `json:"stream_hash"`
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2020-12-23 23:08:13 +01:00
|
|
|
type trackAccess int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Don't track accesses
|
|
|
|
TrackAccessNone trackAccess = iota
|
|
|
|
// Track accesses at the stream level
|
|
|
|
TrackAccessStreams
|
|
|
|
// Track accesses at the blob level
|
|
|
|
TrackAccessBlobs
|
|
|
|
)
|
|
|
|
|
2018-08-07 22:51:02 +02:00
|
|
|
// SQL implements the DB interface
|
2018-01-31 02:15:21 +01:00
|
|
|
type SQL struct {
|
|
|
|
conn *sql.DB
|
2020-10-05 20:05:00 +02:00
|
|
|
|
2020-12-23 23:08:13 +01:00
|
|
|
// Track the approx last time a blob or stream was accessed
|
|
|
|
TrackAccess trackAccess
|
|
|
|
|
|
|
|
// Instead of deleting a blob, marked it as not stored in the db
|
|
|
|
SoftDelete bool
|
2021-01-06 16:43:35 +01:00
|
|
|
|
|
|
|
// Log executed queries. qt.InterpolateParams is cpu-heavy. This avoids that call if not needed.
|
|
|
|
LogQueries bool
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 16:43:35 +01:00
|
|
|
func (s SQL) logQuery(query string, args ...interface{}) {
|
|
|
|
if !s.LogQueries {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
qStr, err := qt.InterpolateParams(query, args...)
|
2018-01-31 02:15:21 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
} else {
|
2021-01-06 16:43:35 +01:00
|
|
|
log.Debugln(qStr)
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Connect will create a connection to the database
|
2018-01-31 02:15:21 +01:00
|
|
|
func (s *SQL) Connect(dsn string) error {
|
|
|
|
var err error
|
2018-08-07 22:51:02 +02:00
|
|
|
// interpolateParams is necessary. otherwise uploading a stream with thousands of blobs
|
|
|
|
// will hit MySQL's max_prepared_stmt_count limit because the prepared statements are all
|
|
|
|
// opened inside a transaction. closing them manually doesn't seem to help
|
|
|
|
dsn += "?parseTime=1&collation=utf8mb4_unicode_ci&interpolateParams=1"
|
2018-01-31 02:15:21 +01:00
|
|
|
s.conn, err = sql.Open("mysql", dsn)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:02:37 +02:00
|
|
|
s.conn.SetMaxIdleConns(12)
|
|
|
|
|
2018-01-31 02:15:21 +01:00
|
|
|
return errors.Err(s.conn.Ping())
|
|
|
|
}
|
|
|
|
|
2018-08-07 22:51:02 +02:00
|
|
|
// AddBlob adds a blob to the database.
|
2018-06-19 19:47:13 +02:00
|
|
|
func (s *SQL) AddBlob(hash string, length int, isStored bool) error {
|
2018-01-31 02:15:21 +01:00
|
|
|
if s.conn == nil {
|
|
|
|
return errors.Err("not connected")
|
|
|
|
}
|
|
|
|
|
2019-07-04 19:57:39 +02:00
|
|
|
_, err := s.insertBlob(hash, length, isStored)
|
|
|
|
return err
|
2018-03-01 22:12:53 +01:00
|
|
|
}
|
|
|
|
|
2020-12-30 04:24:11 +01:00
|
|
|
// AddBlob adds a blob to the database.
|
|
|
|
func (s *SQL) AddBlobs(hash []string) error {
|
|
|
|
if s.conn == nil {
|
|
|
|
return errors.Err("not connected")
|
|
|
|
}
|
|
|
|
// Split the slice into batches of 20 items.
|
|
|
|
batch := 10000
|
2021-01-07 01:26:30 +01:00
|
|
|
totalBlobs := int64(len(hash))
|
|
|
|
work := make(chan []string, 1000)
|
|
|
|
stopper := stop.New()
|
|
|
|
var totalInserted atomic.Int64
|
|
|
|
start := time.Now()
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < len(hash); i += batch {
|
|
|
|
j := i + batch
|
|
|
|
if j > len(hash) {
|
|
|
|
j = len(hash)
|
|
|
|
}
|
|
|
|
work <- hash[i:j]
|
2020-12-30 04:24:11 +01:00
|
|
|
}
|
2021-01-07 01:26:30 +01:00
|
|
|
log.Infof("done loading %d hashes in the work queue", len(hash))
|
|
|
|
close(work)
|
|
|
|
}()
|
|
|
|
for i := 0; i < runtime.NumCPU(); i++ {
|
|
|
|
stopper.Add(1)
|
|
|
|
go func(worker int) {
|
|
|
|
log.Infof("starting worker %d", worker)
|
|
|
|
defer stopper.Done()
|
|
|
|
for hashes := range work {
|
|
|
|
inserted := totalInserted.Load()
|
|
|
|
remaining := totalBlobs - inserted
|
|
|
|
if inserted > 0 {
|
|
|
|
timePerBlob := time.Since(start).Microseconds() / inserted
|
|
|
|
remainingTime := time.Duration(remaining*timePerBlob) * time.Microsecond
|
|
|
|
log.Infof("[T%d] processing batch of %d items. ETA: %s", worker, len(hashes), remainingTime.String())
|
|
|
|
}
|
|
|
|
err := s.insertBlobs(hashes) // Process the batch.
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while inserting batch: %s", errors.FullTrace(err))
|
|
|
|
}
|
|
|
|
totalInserted.Add(int64(len(hashes)))
|
|
|
|
}
|
|
|
|
}(i)
|
2020-12-30 04:24:11 +01:00
|
|
|
}
|
2021-01-07 01:26:30 +01:00
|
|
|
stopper.Wait()
|
2020-12-30 04:24:11 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-05 17:36:33 +01:00
|
|
|
func (s *SQL) insertBlobs(hashes []string) error {
|
|
|
|
var (
|
2021-01-07 01:26:30 +01:00
|
|
|
q string
|
|
|
|
//args []interface{}
|
2021-01-05 17:36:33 +01:00
|
|
|
)
|
2021-01-07 01:26:30 +01:00
|
|
|
dayAgo := time.Now().AddDate(0, 0, -1).Format("2006-01-02 15:04:05")
|
2021-01-05 17:36:33 +01:00
|
|
|
q = "insert into blob_ (hash, is_stored, length, last_accessed_at) values "
|
|
|
|
for _, hash := range hashes {
|
2021-01-07 01:26:30 +01:00
|
|
|
// prepared statements slow everything down by a lot due to reflection
|
|
|
|
// for this specific instance we'll go ahead and hardcode the query to make it go faster
|
|
|
|
q += fmt.Sprintf("('%s',1,%d,'%s'),", hash, stream.MaxBlobSize, dayAgo)
|
|
|
|
//args = append(args, hash, true, stream.MaxBlobSize, dayAgo)
|
2021-01-05 17:36:33 +01:00
|
|
|
}
|
|
|
|
q = strings.TrimSuffix(q, ",")
|
2021-01-07 01:26:30 +01:00
|
|
|
_, err := s.exec(q)
|
2021-01-05 17:36:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:35:43 +02:00
|
|
|
func (s *SQL) makeInsertStreamTransaction(sdBlob SdBlob, streamID int64) (txFunc, error) {
|
|
|
|
return func(tx *sql.Tx) error {
|
|
|
|
for _, contentBlob := range sdBlob.Blobs {
|
|
|
|
if contentBlob.BlobHash == "" {
|
|
|
|
// null terminator blob
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
q string
|
|
|
|
args []interface{}
|
|
|
|
)
|
|
|
|
if s.TrackAccess == TrackAccessBlobs {
|
|
|
|
args = []interface{}{contentBlob.BlobHash, false, contentBlob.Length, time.Now()}
|
|
|
|
q = "INSERT INTO blob_ (hash, is_stored, length, last_accessed_at) VALUES (" + qt.Qs(len(args)) + ") ON DUPLICATE KEY UPDATE is_stored = (is_stored or VALUES(is_stored)), last_accessed_at = VALUES(last_accessed_at)"
|
|
|
|
} else {
|
|
|
|
args = []interface{}{contentBlob.BlobHash, false, contentBlob.Length}
|
|
|
|
q = "INSERT INTO blob_ (hash, is_stored, length) VALUES (" + qt.Qs(len(args)) + ") ON DUPLICATE KEY UPDATE is_stored = (is_stored or VALUES(is_stored))"
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := tx.Exec(q, args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobID, err := result.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if blobID == 0 {
|
|
|
|
err = tx.QueryRow("SELECT id FROM blob_ WHERE hash = ?", contentBlob.BlobHash).Scan(&blobID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
if blobID == 0 {
|
|
|
|
return errors.Err("blob ID is 0 even after INSERTing and SELECTing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.TrackAccess == TrackAccessBlobs {
|
|
|
|
err := s.touchBlobs([]uint64{uint64(blobID)})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
streamArgs := []interface{}{streamID, blobID, contentBlob.BlobNum}
|
|
|
|
_, err = tx.Exec(
|
|
|
|
"INSERT IGNORE INTO stream_blob (stream_id, blob_id, num) VALUES ("+qt.Qs(len(args))+")",
|
|
|
|
streamArgs...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-04 19:57:39 +02:00
|
|
|
func (s *SQL) insertBlob(hash string, length int, isStored bool) (int64, error) {
|
2018-01-31 02:15:21 +01:00
|
|
|
if length <= 0 {
|
2019-07-04 19:57:39 +02:00
|
|
|
return 0, errors.Err("length must be positive")
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2020-12-23 23:08:13 +01:00
|
|
|
var (
|
|
|
|
q string
|
|
|
|
args []interface{}
|
2018-08-07 22:51:02 +02:00
|
|
|
)
|
2020-12-23 23:08:13 +01:00
|
|
|
if s.TrackAccess == TrackAccessBlobs {
|
|
|
|
args = []interface{}{hash, isStored, length, time.Now()}
|
|
|
|
q = "INSERT INTO blob_ (hash, is_stored, length, last_accessed_at) VALUES (" + qt.Qs(len(args)) + ") ON DUPLICATE KEY UPDATE is_stored = (is_stored or VALUES(is_stored)), last_accessed_at = VALUES(last_accessed_at)"
|
|
|
|
} else {
|
|
|
|
args = []interface{}{hash, isStored, length}
|
|
|
|
q = "INSERT INTO blob_ (hash, is_stored, length) VALUES (" + qt.Qs(len(args)) + ") ON DUPLICATE KEY UPDATE is_stored = (is_stored or VALUES(is_stored))"
|
|
|
|
}
|
|
|
|
|
|
|
|
blobID, err := s.exec(q, args...)
|
2018-01-31 02:15:21 +01:00
|
|
|
if err != nil {
|
2019-07-04 19:57:39 +02:00
|
|
|
return 0, err
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2019-07-04 19:57:39 +02:00
|
|
|
if blobID == 0 {
|
|
|
|
err = s.conn.QueryRow("SELECT id FROM blob_ WHERE hash = ?", hash).Scan(&blobID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Err(err)
|
|
|
|
}
|
|
|
|
if blobID == 0 {
|
|
|
|
return 0, errors.Err("blob ID is 0 even after INSERTing and SELECTing")
|
|
|
|
}
|
2020-12-23 23:08:13 +01:00
|
|
|
|
|
|
|
if s.TrackAccess == TrackAccessBlobs {
|
|
|
|
err := s.touchBlobs([]uint64{uint64(blobID)})
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Err(err)
|
|
|
|
}
|
|
|
|
}
|
2019-07-04 19:57:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return blobID, nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 00:03:56 +02:00
|
|
|
func (s *SQL) insertStream(hash string, sdBlobID int64) (int64, error) {
|
2020-12-23 23:08:13 +01:00
|
|
|
var (
|
|
|
|
q string
|
|
|
|
args []interface{}
|
2019-07-04 19:57:39 +02:00
|
|
|
)
|
2020-12-23 23:08:13 +01:00
|
|
|
|
|
|
|
if s.TrackAccess == TrackAccessStreams {
|
|
|
|
args = []interface{}{hash, sdBlobID, time.Now()}
|
|
|
|
q = "INSERT IGNORE INTO stream (hash, sd_blob_id, last_accessed_at) VALUES (" + qt.Qs(len(args)) + ")"
|
|
|
|
} else {
|
|
|
|
args = []interface{}{hash, sdBlobID}
|
|
|
|
q = "INSERT IGNORE INTO stream (hash, sd_blob_id) VALUES (" + qt.Qs(len(args)) + ")"
|
|
|
|
}
|
|
|
|
|
|
|
|
streamID, err := s.exec(q, args...)
|
2019-07-04 19:57:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if streamID == 0 {
|
|
|
|
err = s.conn.QueryRow("SELECT id FROM stream WHERE sd_blob_id = ?", sdBlobID).Scan(&streamID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Err(err)
|
|
|
|
}
|
|
|
|
if streamID == 0 {
|
|
|
|
return 0, errors.Err("stream ID is 0 even after INSERTing and SELECTing")
|
|
|
|
}
|
2020-10-05 20:05:00 +02:00
|
|
|
|
2020-12-23 23:08:13 +01:00
|
|
|
if s.TrackAccess == TrackAccessStreams {
|
|
|
|
err := s.touchStreams([]uint64{uint64(streamID)})
|
2020-10-05 20:05:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Err(err)
|
|
|
|
}
|
|
|
|
}
|
2019-07-04 19:57:39 +02:00
|
|
|
}
|
|
|
|
return streamID, nil
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// HasBlob checks if the database contains the blob information.
|
2021-01-05 17:36:33 +01:00
|
|
|
func (s *SQL) HasBlob(hash string, touch bool) (bool, error) {
|
|
|
|
exists, err := s.HasBlobs([]string{hash}, touch)
|
2018-09-20 17:24:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
2018-09-20 17:24:36 +02:00
|
|
|
return exists[hash], nil
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
2018-02-02 22:49:20 +01:00
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// HasBlobs checks if the database contains the set of blobs and returns a bool map.
|
2021-01-05 17:36:33 +01:00
|
|
|
func (s *SQL) HasBlobs(hashes []string, touch bool) (map[string]bool, error) {
|
2020-12-23 23:08:13 +01:00
|
|
|
exists, idsNeedingTouch, err := s.hasBlobs(hashes)
|
2021-01-05 17:36:33 +01:00
|
|
|
|
|
|
|
if touch {
|
2020-12-30 04:24:11 +01:00
|
|
|
if s.TrackAccess == TrackAccessBlobs {
|
|
|
|
s.touchBlobs(idsNeedingTouch)
|
|
|
|
} else if s.TrackAccess == TrackAccessStreams {
|
|
|
|
s.touchStreams(idsNeedingTouch)
|
|
|
|
}
|
2021-01-05 17:36:33 +01:00
|
|
|
}
|
2020-12-23 23:08:13 +01:00
|
|
|
|
2020-10-05 20:05:00 +02:00
|
|
|
return exists, err
|
|
|
|
}
|
|
|
|
|
2020-12-23 23:08:13 +01:00
|
|
|
func (s *SQL) touchBlobs(blobIDs []uint64) error {
|
|
|
|
if len(blobIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
query := "UPDATE blob_ SET last_accessed_at = ? WHERE id IN (" + qt.Qs(len(blobIDs)) + ")"
|
|
|
|
args := make([]interface{}, len(blobIDs)+1)
|
|
|
|
args[0] = time.Now()
|
|
|
|
for i := range blobIDs {
|
|
|
|
args[i+1] = blobIDs[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
_, err := s.exec(query, args...)
|
|
|
|
log.Debugf("touched %d blobs and took %s", len(blobIDs), time.Since(startTime))
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQL) touchStreams(streamIDs []uint64) error {
|
2020-10-05 20:05:00 +02:00
|
|
|
if len(streamIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
query := "UPDATE stream SET last_accessed_at = ? WHERE id IN (" + qt.Qs(len(streamIDs)) + ")"
|
|
|
|
args := make([]interface{}, len(streamIDs)+1)
|
|
|
|
args[0] = time.Now()
|
|
|
|
for i := range streamIDs {
|
|
|
|
args[i+1] = streamIDs[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
_, err := s.exec(query, args...)
|
2020-12-23 23:08:13 +01:00
|
|
|
log.Debugf("touched %d streams and took %s", len(streamIDs), time.Since(startTime))
|
2020-10-05 20:05:00 +02:00
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQL) hasBlobs(hashes []string) (map[string]bool, []uint64, error) {
|
2018-05-15 02:55:45 +02:00
|
|
|
if s.conn == nil {
|
2020-10-05 20:05:00 +02:00
|
|
|
return nil, nil, errors.Err("not connected")
|
2018-05-15 02:55:45 +02:00
|
|
|
}
|
|
|
|
|
2020-10-05 20:05:00 +02:00
|
|
|
var (
|
|
|
|
hash string
|
2020-12-24 23:13:31 +01:00
|
|
|
blobID uint64
|
|
|
|
streamID null.Uint64
|
2020-10-06 15:39:43 +02:00
|
|
|
lastAccessedAt null.Time
|
2020-10-05 20:05:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var needsTouch []uint64
|
2018-05-15 02:55:45 +02:00
|
|
|
exists := make(map[string]bool)
|
2020-10-05 20:05:00 +02:00
|
|
|
|
2021-01-07 01:26:30 +01:00
|
|
|
touchDeadline := time.Now().Add(-6 * time.Hour) // touch blob if last accessed before this time
|
2019-06-25 14:44:30 +02:00
|
|
|
maxBatchSize := 10000
|
2018-05-15 02:55:45 +02:00
|
|
|
doneIndex := 0
|
|
|
|
|
|
|
|
for len(hashes) > doneIndex {
|
|
|
|
sliceEnd := doneIndex + maxBatchSize
|
|
|
|
if sliceEnd > len(hashes) {
|
|
|
|
sliceEnd = len(hashes)
|
|
|
|
}
|
|
|
|
log.Debugf("getting hashes[%d:%d] of %d", doneIndex, sliceEnd, len(hashes))
|
|
|
|
batch := hashes[doneIndex:sliceEnd]
|
|
|
|
|
2021-01-05 18:16:44 +01:00
|
|
|
var query string
|
2020-12-23 23:08:13 +01:00
|
|
|
if s.TrackAccess == TrackAccessBlobs {
|
2021-01-05 18:16:44 +01:00
|
|
|
query = `SELECT b.hash, b.id, NULL, b.last_accessed_at
|
|
|
|
FROM blob_ b
|
|
|
|
WHERE b.is_stored = 1 and b.hash IN (` + qt.Qs(len(batch)) + `)`
|
2020-12-23 23:08:13 +01:00
|
|
|
} else if s.TrackAccess == TrackAccessStreams {
|
2021-01-05 18:16:44 +01:00
|
|
|
query = `SELECT b.hash, b.id, s.id, s.last_accessed_at
|
2020-10-05 20:05:00 +02:00
|
|
|
FROM blob_ b
|
|
|
|
LEFT JOIN stream_blob sb ON b.id = sb.blob_id
|
2021-01-05 18:16:44 +01:00
|
|
|
INNER JOIN stream s on (sb.stream_id = s.id or s.sd_blob_id = b.id)
|
2020-12-23 23:08:13 +01:00
|
|
|
WHERE b.is_stored = 1 and b.hash IN (` + qt.Qs(len(batch)) + `)`
|
2021-01-05 18:16:44 +01:00
|
|
|
} else {
|
|
|
|
query = `SELECT b.hash, b.id, NULL, NULL
|
|
|
|
FROM blob_ b
|
|
|
|
WHERE b.is_stored = 1 and b.hash IN (` + qt.Qs(len(batch)) + `)`
|
|
|
|
}
|
|
|
|
|
2020-12-23 23:08:13 +01:00
|
|
|
args := make([]interface{}, len(batch))
|
2018-05-15 02:55:45 +02:00
|
|
|
for i := range batch {
|
2020-12-23 23:08:13 +01:00
|
|
|
args[i] = batch[i]
|
2018-05-15 02:55:45 +02:00
|
|
|
}
|
|
|
|
|
2021-01-06 16:43:35 +01:00
|
|
|
s.logQuery(query, args...)
|
2018-05-15 02:55:45 +02:00
|
|
|
|
2018-09-20 17:24:36 +02:00
|
|
|
err := func() error {
|
2019-06-25 15:13:25 +02:00
|
|
|
startTime := time.Now()
|
2018-09-20 17:24:36 +02:00
|
|
|
rows, err := s.conn.Query(query, args...)
|
2019-06-25 15:13:25 +02:00
|
|
|
log.Debugf("hashes query took %s", time.Since(startTime))
|
2018-09-20 17:24:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
defer closeRows(rows)
|
|
|
|
|
|
|
|
for rows.Next() {
|
2020-12-23 23:08:13 +01:00
|
|
|
err := rows.Scan(&hash, &blobID, &streamID, &lastAccessedAt)
|
2018-09-20 17:24:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
exists[hash] = true
|
2020-12-23 23:08:13 +01:00
|
|
|
if !lastAccessedAt.Valid || lastAccessedAt.Time.Before(touchDeadline) {
|
|
|
|
if s.TrackAccess == TrackAccessBlobs {
|
|
|
|
needsTouch = append(needsTouch, blobID)
|
2020-12-24 23:13:31 +01:00
|
|
|
} else if s.TrackAccess == TrackAccessStreams && !streamID.IsZero() {
|
|
|
|
needsTouch = append(needsTouch, streamID.Uint64)
|
2020-12-23 23:08:13 +01:00
|
|
|
}
|
2020-10-05 20:05:00 +02:00
|
|
|
}
|
2018-09-20 17:24:36 +02:00
|
|
|
}
|
2018-05-15 02:55:45 +02:00
|
|
|
|
2018-09-20 17:24:36 +02:00
|
|
|
err = rows.Err()
|
2018-05-15 02:55:45 +02:00
|
|
|
if err != nil {
|
2018-09-20 17:24:36 +02:00
|
|
|
return errors.Err(err)
|
2018-05-15 02:55:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 17:24:36 +02:00
|
|
|
doneIndex += len(batch)
|
|
|
|
return nil
|
|
|
|
}()
|
2018-05-15 02:55:45 +02:00
|
|
|
if err != nil {
|
2020-10-05 20:05:00 +02:00
|
|
|
return nil, nil, err
|
2018-05-15 02:55:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 20:05:00 +02:00
|
|
|
return exists, needsTouch, nil
|
2018-05-15 02:55:45 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 23:08:13 +01:00
|
|
|
// Delete will remove (or soft-delete) the blob from the db
|
|
|
|
// NOTE: If SoftDelete is enabled, streams will never be deleted
|
2018-09-11 13:41:29 +02:00
|
|
|
func (s *SQL) Delete(hash string) error {
|
2020-12-23 23:08:13 +01:00
|
|
|
if s.SoftDelete {
|
|
|
|
_, err := s.exec("UPDATE blob_ SET is_stored = 0 WHERE hash = ?", hash)
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
2019-07-31 00:03:56 +02:00
|
|
|
_, err := s.exec("DELETE FROM stream WHERE sd_blob_id = (SELECT id FROM blob_ WHERE hash = ?)", hash)
|
2019-06-27 21:30:38 +02:00
|
|
|
if err != nil {
|
2018-09-11 13:41:29 +02:00
|
|
|
return errors.Err(err)
|
2019-06-27 21:30:38 +02:00
|
|
|
}
|
|
|
|
|
2019-07-04 19:57:39 +02:00
|
|
|
_, err = s.exec("DELETE FROM blob_ WHERE hash = ?", hash)
|
2019-06-27 21:30:38 +02:00
|
|
|
return errors.Err(err)
|
2018-09-11 13:41:29 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 23:08:13 +01:00
|
|
|
// GetHashRange gets the smallest and biggest hashes in the db
|
|
|
|
func (s *SQL) LeastRecentlyAccessedHashes(maxBlobs int) ([]string, error) {
|
|
|
|
if s.conn == nil {
|
|
|
|
return nil, errors.Err("not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.TrackAccess != TrackAccessBlobs {
|
|
|
|
return nil, errors.Err("blob access tracking is disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
query := "SELECT hash from blob_ where is_stored = 1 order by last_accessed_at limit ?"
|
2021-01-06 16:43:35 +01:00
|
|
|
s.logQuery(query, maxBlobs)
|
2020-12-23 23:08:13 +01:00
|
|
|
|
|
|
|
rows, err := s.conn.Query(query, maxBlobs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
defer closeRows(rows)
|
|
|
|
|
|
|
|
blobs := make([]string, 0, maxBlobs)
|
|
|
|
for rows.Next() {
|
|
|
|
var hash string
|
|
|
|
err := rows.Scan(&hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
blobs = append(blobs, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllHashes writes all hashes from the db into the channel.
|
|
|
|
// It does not close the channel when it finishes.
|
|
|
|
//func (s *SQL) AllHashes(ch chan<- string) error {
|
|
|
|
// if s.conn == nil {
|
|
|
|
// return errors.Err("not connected")
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// query := "SELECT hash from blob_"
|
|
|
|
// if s.SoftDelete {
|
|
|
|
// query += " where is_stored = 1"
|
|
|
|
// }
|
2021-01-06 16:43:35 +01:00
|
|
|
// s.logQuery(query)
|
2020-12-23 23:08:13 +01:00
|
|
|
//
|
|
|
|
// rows, err := s.conn.Query(query)
|
|
|
|
// if err != nil {
|
|
|
|
// return errors.Err(err)
|
|
|
|
// }
|
|
|
|
// defer closeRows(rows)
|
|
|
|
//
|
|
|
|
// for rows.Next() {
|
|
|
|
// var hash string
|
|
|
|
// err := rows.Scan(&hash)
|
|
|
|
// if err != nil {
|
|
|
|
// return errors.Err(err)
|
|
|
|
// }
|
|
|
|
// ch <- hash
|
|
|
|
// // TODO: this needs testing
|
|
|
|
// // TODO: need a way to cancel this early (e.g. in case of shutdown)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// close(ch)
|
|
|
|
// return nil
|
|
|
|
//}
|
|
|
|
|
|
|
|
func (s *SQL) Count() (int, error) {
|
|
|
|
if s.conn == nil {
|
|
|
|
return 0, errors.Err("not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
query := "SELECT count(id) from blob_"
|
|
|
|
if s.SoftDelete {
|
|
|
|
query += " where is_stored = 1"
|
|
|
|
}
|
2021-01-06 16:43:35 +01:00
|
|
|
s.logQuery(query)
|
2020-12-23 23:08:13 +01:00
|
|
|
|
|
|
|
var count int
|
|
|
|
err := s.conn.QueryRow(query).Scan(&count)
|
|
|
|
return count, errors.Err(err)
|
|
|
|
}
|
|
|
|
|
2018-09-11 13:41:29 +02:00
|
|
|
// Block will mark a blob as blocked
|
|
|
|
func (s *SQL) Block(hash string) error {
|
|
|
|
query := "INSERT IGNORE INTO blocked SET hash = ?"
|
|
|
|
args := []interface{}{hash}
|
2021-01-06 16:43:35 +01:00
|
|
|
s.logQuery(query, args...)
|
2018-09-11 13:41:29 +02:00
|
|
|
_, err := s.conn.Exec(query, args...)
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
2018-09-20 17:24:36 +02:00
|
|
|
// GetBlocked will return a list of blocked hashes
|
|
|
|
func (s *SQL) GetBlocked() (map[string]bool, error) {
|
|
|
|
query := "SELECT hash FROM blocked"
|
2021-01-06 16:43:35 +01:00
|
|
|
s.logQuery(query)
|
2018-09-20 17:24:36 +02:00
|
|
|
rows, err := s.conn.Query(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
defer closeRows(rows)
|
|
|
|
|
|
|
|
blocked := make(map[string]bool)
|
|
|
|
|
|
|
|
var hash string
|
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
blocked[hash] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocked, nil
|
|
|
|
}
|
|
|
|
|
2018-08-16 02:17:02 +02:00
|
|
|
// MissingBlobsForKnownStream returns missing blobs for an existing stream
|
|
|
|
// WARNING: if the stream does NOT exist, no blob hashes will be returned, which looks
|
|
|
|
// like no blobs are missing
|
|
|
|
func (s *SQL) MissingBlobsForKnownStream(sdHash string) ([]string, error) {
|
2018-07-26 16:25:47 +02:00
|
|
|
if s.conn == nil {
|
2018-08-16 02:17:02 +02:00
|
|
|
return nil, errors.Err("not connected")
|
2018-07-26 16:25:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 02:17:02 +02:00
|
|
|
query := `
|
|
|
|
SELECT b.hash FROM blob_ b
|
2019-07-31 00:03:56 +02:00
|
|
|
INNER JOIN stream_blob sb ON b.id = sb.blob_id
|
|
|
|
INNER JOIN stream s ON s.id = sb.stream_id
|
|
|
|
INNER JOIN blob_ sdb ON sdb.id = s.sd_blob_id AND sdb.hash = ?
|
2018-08-16 02:17:02 +02:00
|
|
|
WHERE b.is_stored = 0
|
|
|
|
`
|
2018-07-26 16:25:47 +02:00
|
|
|
args := []interface{}{sdHash}
|
|
|
|
|
2021-01-06 16:43:35 +01:00
|
|
|
s.logQuery(query, args...)
|
2018-07-26 16:25:47 +02:00
|
|
|
|
2018-08-16 02:17:02 +02:00
|
|
|
rows, err := s.conn.Query(query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
defer closeRows(rows)
|
2018-07-26 16:25:47 +02:00
|
|
|
|
2018-08-16 02:17:02 +02:00
|
|
|
var missingBlobs []string
|
|
|
|
var hash string
|
2018-07-26 16:25:47 +02:00
|
|
|
|
2018-08-16 02:17:02 +02:00
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
missingBlobs = append(missingBlobs, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return missingBlobs, errors.Err(err)
|
2018-07-26 16:25:47 +02:00
|
|
|
}
|
|
|
|
|
2019-07-04 19:57:39 +02:00
|
|
|
// AddSDBlob insert the SD blob and all the content blobs. The content blobs are marked as "not stored",
|
|
|
|
// but they are tracked so reflector knows what it is missing.
|
2018-08-07 22:51:02 +02:00
|
|
|
func (s *SQL) AddSDBlob(sdHash string, sdBlobLength int, sdBlob SdBlob) error {
|
2018-02-02 22:49:20 +01:00
|
|
|
if s.conn == nil {
|
|
|
|
return errors.Err("not connected")
|
|
|
|
}
|
|
|
|
|
2019-07-04 19:57:39 +02:00
|
|
|
sdBlobID, err := s.insertBlob(sdHash, sdBlobLength, true)
|
2019-06-27 21:30:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-31 00:03:56 +02:00
|
|
|
streamID, err := s.insertStream(sdBlob.StreamHash, sdBlobID)
|
2019-06-27 21:30:38 +02:00
|
|
|
if err != nil {
|
2019-07-04 19:57:39 +02:00
|
|
|
return err
|
2019-06-27 21:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert content blobs and connect them to stream
|
2021-05-20 20:35:43 +02:00
|
|
|
insertTX, err := s.makeInsertStreamTransaction(sdBlob, streamID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = s.withTx(insertTX)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-27 21:30:38 +02:00
|
|
|
}
|
|
|
|
return nil
|
2018-03-01 22:12:53 +01:00
|
|
|
}
|
|
|
|
|
2018-06-19 19:47:13 +02:00
|
|
|
// GetHashRange gets the smallest and biggest hashes in the db
|
|
|
|
func (s *SQL) GetHashRange() (string, string, error) {
|
|
|
|
var min string
|
|
|
|
var max string
|
|
|
|
|
|
|
|
if s.conn == nil {
|
|
|
|
return "", "", errors.Err("not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
query := "SELECT MIN(hash), MAX(hash) from blob_"
|
|
|
|
|
2021-01-06 16:43:35 +01:00
|
|
|
s.logQuery(query)
|
2018-06-19 19:47:13 +02:00
|
|
|
|
|
|
|
err := s.conn.QueryRow(query).Scan(&min, &max)
|
|
|
|
return min, max, err
|
|
|
|
}
|
|
|
|
|
2018-06-22 15:30:16 +02:00
|
|
|
// GetStoredHashesInRange gets stored blobs with hashes in a given range, and sends the hashes into a channel
|
|
|
|
func (s *SQL) GetStoredHashesInRange(ctx context.Context, start, end bits.Bitmap) (ch chan bits.Bitmap, ech chan error) {
|
2018-06-15 04:30:37 +02:00
|
|
|
ch = make(chan bits.Bitmap)
|
|
|
|
ech = make(chan error)
|
|
|
|
|
|
|
|
// TODO: needs waitgroup?
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
defer close(ech)
|
|
|
|
|
|
|
|
if s.conn == nil {
|
|
|
|
ech <- errors.Err("not connected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-08 02:19:04 +02:00
|
|
|
query := "SELECT hash FROM blob_ WHERE hash >= ? AND hash <= ? AND is_stored = 1"
|
|
|
|
args := []interface{}{start.Hex(), end.Hex()}
|
2018-06-15 04:30:37 +02:00
|
|
|
|
2021-01-06 16:43:35 +01:00
|
|
|
s.logQuery(query, args...)
|
2018-06-15 04:30:37 +02:00
|
|
|
|
|
|
|
rows, err := s.conn.Query(query, args...)
|
|
|
|
defer closeRows(rows)
|
|
|
|
if err != nil {
|
|
|
|
ech <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var hash string
|
2018-08-07 17:38:55 +02:00
|
|
|
ScanLoop:
|
2018-06-15 04:30:37 +02:00
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&hash)
|
|
|
|
if err != nil {
|
|
|
|
ech <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2018-08-07 17:38:55 +02:00
|
|
|
break ScanLoop
|
2018-06-15 04:30:37 +02:00
|
|
|
case ch <- bits.FromHexP(hash):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
ech <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-01 22:12:53 +01:00
|
|
|
// txFunc is a function that can be wrapped in a transaction
|
|
|
|
type txFunc func(tx *sql.Tx) error
|
|
|
|
|
|
|
|
// withTx wraps a function in an sql transaction. the transaction is committed if there's no error, or rolled back if there is one.
|
|
|
|
// if dbOrTx is an sql.DB, a new transaction is started
|
2021-05-20 20:14:56 +02:00
|
|
|
func (s *SQL) withTx(f txFunc) (err error) {
|
|
|
|
tx, err := s.conn.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-02 22:49:20 +01:00
|
|
|
}
|
2021-05-20 20:14:56 +02:00
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
|
|
|
if rollBackError := tx.Rollback(); rollBackError != nil {
|
|
|
|
log.Error("failed to rollback tx on panic - ", rollBackError)
|
|
|
|
}
|
|
|
|
panic(p)
|
|
|
|
} else if err != nil {
|
|
|
|
if rollBackError := tx.Rollback(); rollBackError != nil {
|
|
|
|
log.Error("failed to rollback tx on panic - ", rollBackError)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = errors.Err(tx.Commit())
|
|
|
|
}
|
|
|
|
}()
|
2018-02-02 22:49:20 +01:00
|
|
|
|
2018-03-01 22:12:53 +01:00
|
|
|
return f(tx)
|
2018-02-02 22:49:20 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
func closeRows(rows *sql.Rows) {
|
2018-06-15 04:30:37 +02:00
|
|
|
if rows != nil {
|
|
|
|
err := rows.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("error closing rows: ", err)
|
|
|
|
}
|
2018-05-30 03:38:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 19:57:39 +02:00
|
|
|
func (s *SQL) exec(query string, args ...interface{}) (int64, error) {
|
2021-01-06 16:43:35 +01:00
|
|
|
s.logQuery(query, args...)
|
2019-06-26 20:36:05 +02:00
|
|
|
attempt, maxAttempts := 0, 3
|
|
|
|
Retry:
|
|
|
|
attempt++
|
2019-07-04 19:57:39 +02:00
|
|
|
result, err := s.conn.Exec(query, args...)
|
|
|
|
if isLockTimeoutError(err) {
|
2019-06-27 21:34:12 +02:00
|
|
|
if attempt <= maxAttempts {
|
|
|
|
//Error 1205: Lock wait timeout exceeded; try restarting transaction
|
|
|
|
goto Retry
|
|
|
|
}
|
2019-07-04 19:57:39 +02:00
|
|
|
err = errors.Prefix("Lock timeout for query "+query, err)
|
2019-06-26 20:36:05 +02:00
|
|
|
}
|
2019-07-04 19:57:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lastID, err := result.LastInsertId()
|
|
|
|
return lastID, errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLockTimeoutError(err error) bool {
|
|
|
|
e, ok := err.(*mysql.MySQLError)
|
|
|
|
return ok && e != nil && e.Number == 1205
|
2018-08-07 22:51:02 +02:00
|
|
|
}
|
|
|
|
|
2018-06-15 04:30:37 +02:00
|
|
|
/* SQL schema
|
|
|
|
|
2018-09-26 22:00:19 +02:00
|
|
|
in prod, set tx_isolation to READ-COMMITTED to improve db performance
|
2019-07-04 19:57:39 +02:00
|
|
|
make sure you use latin1 or utf8 charset, NOT utf8mb4. that's a waste of space.
|
|
|
|
|
|
|
|
todo: could add UNIQUE KEY (stream_hash, num) to stream_blob ...
|
2018-09-26 22:00:19 +02:00
|
|
|
|
2018-02-02 22:49:20 +01:00
|
|
|
CREATE TABLE blob_ (
|
2019-07-04 19:57:39 +02:00
|
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
|
2018-02-02 22:49:20 +01:00
|
|
|
hash char(96) NOT NULL,
|
2018-06-19 19:47:13 +02:00
|
|
|
is_stored TINYINT(1) NOT NULL DEFAULT 0,
|
2018-02-02 22:49:20 +01:00
|
|
|
length bigint(20) unsigned DEFAULT NULL,
|
2020-12-23 23:08:13 +01:00
|
|
|
last_accessed_at TIMESTAMP NULL DEFAULT NULL,
|
2019-07-04 19:57:39 +02:00
|
|
|
PRIMARY KEY (id),
|
2020-12-24 23:13:31 +01:00
|
|
|
UNIQUE KEY blob_hash_idx (hash),
|
2021-04-29 03:41:18 +02:00
|
|
|
KEY `blob_last_accessed_idx` (`last_accessed_at`),
|
|
|
|
KEY `is_stored_idx` (`is_stored`)
|
2018-06-15 04:30:37 +02:00
|
|
|
);
|
2018-02-02 22:49:20 +01:00
|
|
|
|
|
|
|
CREATE TABLE stream (
|
2019-07-04 19:57:39 +02:00
|
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
|
2018-02-02 22:49:20 +01:00
|
|
|
hash char(96) NOT NULL,
|
2019-07-04 19:57:39 +02:00
|
|
|
sd_blob_id BIGINT UNSIGNED NOT NULL,
|
2020-10-05 20:05:00 +02:00
|
|
|
last_accessed_at TIMESTAMP NULL DEFAULT NULL,
|
2019-07-04 19:57:39 +02:00
|
|
|
PRIMARY KEY (id),
|
|
|
|
UNIQUE KEY stream_hash_idx (hash),
|
|
|
|
KEY stream_sd_blob_id_idx (sd_blob_id),
|
2020-10-05 20:05:00 +02:00
|
|
|
KEY last_accessed_at_idx (last_accessed_at),
|
2019-07-04 19:57:39 +02:00
|
|
|
FOREIGN KEY (sd_blob_id) REFERENCES blob_ (id) ON DELETE RESTRICT ON UPDATE CASCADE
|
2018-06-15 04:30:37 +02:00
|
|
|
);
|
2018-02-02 22:49:20 +01:00
|
|
|
|
|
|
|
CREATE TABLE stream_blob (
|
2019-07-04 19:57:39 +02:00
|
|
|
stream_id BIGINT UNSIGNED NOT NULL,
|
|
|
|
blob_id BIGINT UNSIGNED NOT NULL,
|
2018-02-02 22:49:20 +01:00
|
|
|
num int NOT NULL,
|
2019-07-04 19:57:39 +02:00
|
|
|
PRIMARY KEY (stream_id, blob_id),
|
|
|
|
KEY stream_blob_blob_id_idx (blob_id),
|
|
|
|
FOREIGN KEY (stream_id) REFERENCES stream (id) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
|
|
FOREIGN KEY (blob_id) REFERENCES blob_ (id) ON DELETE CASCADE ON UPDATE CASCADE
|
2018-06-15 04:30:37 +02:00
|
|
|
);
|
|
|
|
|
2018-09-11 13:41:29 +02:00
|
|
|
CREATE TABLE blocked (
|
|
|
|
hash char(96) NOT NULL,
|
|
|
|
PRIMARY KEY (hash)
|
|
|
|
);
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
*/
|
2020-12-23 23:08:13 +01:00
|
|
|
|
|
|
|
//func (d *LiteDBBackedStore) selfClean() {
|
|
|
|
// d.stopper.Add(1)
|
|
|
|
// defer d.stopper.Done()
|
|
|
|
// lastCleanup := time.Now()
|
|
|
|
// const cleanupInterval = 10 * time.Second
|
|
|
|
// for {
|
|
|
|
// select {
|
|
|
|
// case <-d.stopper.Ch():
|
|
|
|
// log.Infoln("stopping self cleanup")
|
|
|
|
// return
|
|
|
|
// default:
|
|
|
|
// time.Sleep(1 * time.Second)
|
|
|
|
// }
|
|
|
|
// if time.Since(lastCleanup) < cleanupInterval {
|
|
|
|
// continue
|
|
|
|
//
|
|
|
|
// blobsCount, err := d.db.BlobsCount()
|
|
|
|
// if err != nil {
|
|
|
|
// log.Errorf(errors.FullTrace(err))
|
|
|
|
// }
|
|
|
|
// if blobsCount >= d.maxItems {
|
|
|
|
// itemsToDelete := blobsCount / 100 * 10
|
|
|
|
// blobs, err := d.db.GetLRUBlobs(itemsToDelete)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Errorf(errors.FullTrace(err))
|
|
|
|
// }
|
|
|
|
// for _, hash := range blobs {
|
|
|
|
// select {
|
|
|
|
// case <-d.stopper.Ch():
|
|
|
|
// return
|
|
|
|
// default:
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// err = d.Delete(hash)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Errorf(errors.FullTrace(err))
|
|
|
|
// }
|
|
|
|
// metrics.CacheLRUEvictCount.With(metrics.CacheLabels(d.Name(), d.component)).Inc()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// lastCleanup = time.Now()
|
|
|
|
// }
|
|
|
|
//}
|