<commit_before>package main

import (
	"encoding/json"
	"errors"
	"log"
	"net/http"
	"sync"
)

var errNoVersions = errors.New("no versions available")

type db struct {
	sequins *sequins

	name        string
	mux         *versionMux
	refreshLock sync.Mutex
	status      dbStatus
}

type dbStatus struct {
	CurrentVersion versionStatus            `json:"current_version"`
	Versions       map[string]versionStatus `json:"versions"`
}

type versionStatus struct {
	Path    string `json:"path"`
	Created int64  `json:"created"`
	State   string `json:"state"`
}

func newDB(sequins *sequins, name string) *db {
	return &db{
		sequins: sequins,
		name:    name,
		mux:     newVersionMux(),
	}
}

func (db *db) refresh() error {
	db.refreshLock.Lock()
	defer db.refreshLock.Unlock()

	versions, err := db.sequins.backend.ListVersions(db.name, db.sequins.config.RequireSuccessFile)
	if err != nil {
		return err
	} else if len(versions) == 0 {
		return errNoVersions
	}

	latestVersion := versions[len(versions)-1]
	currentVersion := db.mux.getCurrent()
	db.mux.release(currentVersion)
	if currentVersion != nil && latestVersion == currentVersion.name {
		// TODO: we log this a bunch uneccessarily.
		log.Println(latestVersion, "is already the newest version of", db.name, "so not reloading.")
		return nil
	}

	files, err := db.sequins.backend.ListFiles(db.name, latestVersion)
	if err != nil {
		return err
	}

	builder := newVersion(db.sequins, db.name, latestVersion, len(files))
	vs, err := builder.build()
	if err != nil {
		return err
	}

	// Prepare the version, so that during the switching period we can respond
	// to requests for it.
	db.mux.prepare(vs)

	// Then, wait for all our peers to be ready. All peers should all see that
	// everything is ready at roughly the same time. If they switch before us,
	// that's fine; we can serve the new version we've 'prepared' when peers<commit_msg>Don't log 'we already have the latest verion' so much<commit_after>package main

import (
	"encoding/json"
	"errors"
	"log"
	"net/http"
	"sync"
)

var errNoVersions = errors.New("no versions available")

type db struct {
	sequins *sequins

	name        string
	mux         *versionMux
	refreshLock sync.Mutex
	status      dbStatus
}

type dbStatus struct {
	CurrentVersion versionStatus            `json:"current_version"`
	Versions       map[string]versionStatus `json:"versions"`
}

type versionStatus struct {
	Path    string `json:"path"`
	Created int64  `json:"created"`
	State   string `json:"state"`
}

func newDB(sequins *sequins, name string) *db {
	return &db{
		sequins: sequins,
		name:    name,
		mux:     newVersionMux(),
	}
}

func (db *db) refresh() error {
	db.refreshLock.Lock()
	defer db.refreshLock.Unlock()

	versions, err := db.sequins.backend.ListVersions(db.name, db.sequins.config.RequireSuccessFile)
	if err != nil {
		return err
	} else if len(versions) == 0 {
		return errNoVersions
	}

	latestVersion := versions[len(versions)-1]
	currentVersion := db.mux.getCurrent()
	db.mux.release(currentVersion)
	if currentVersion != nil && latestVersion == currentVersion.name {
		return nil
	}

	files, err := db.sequins.backend.ListFiles(db.name, latestVersion)
	if err != nil {
		return err
	}

	builder := newVersion(db.sequins, db.name, latestVersion, len(files))
	vs, err := builder.build()
	if err != nil {
		return err
	}

	// Prepare the version, so that during the switching period we can respond
	// to requests for it.
	db.mux.prepare(vs)

	// Then, wait for all our peers to be ready. All peers should all see that
	// everything is ready at roughly the same time. If they switch before us,
	// that's fine; we can serve the new version we've 'prepared' when peers
{'fraction_non_alphanumeric': 0.11345789331167073, 'fraction_numerical': 0.002166260492824262, 'mean_word_length': 3.6348808030112925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1897759', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1191, 'n_words': 376}