<commit_before>package nail

import (
	"log"

	"github.com/antihax/evedata/internal/sqlhelper"

	sq "github.com/Masterminds/squirrel"
	"github.com/antihax/evedata/internal/datapackages"
	"github.com/antihax/evedata/internal/gobcoder"
	nsq "github.com/nsqio/go-nsq"
)

func init() {
	AddHandler("characterAssets", func(s *Nail, consumer *nsq.Consumer) {
		consumer.AddConcurrentHandlers(s.wait(nsq.HandlerFunc(s.characterAssetsConsumer)), 5)
	})
}

func (s *Nail) characterAssetsConsumer(message *nsq.Message) error {
	assets := datapackages.CharacterAssets{}
	err := gobcoder.GobDecoder(message.Body, &assets)
	if err != nil {
		log.Println(err)
		return err
	}

	// Start a new transaction to keep the delete an inserts together
	tx, err := s.db.Beginx()
	if err != nil {
		log.Println(err)
		return err
	}
	defer tx.Rollback()

	// Delete everything because we will replace with the new assets.
	_, err = tx.Exec("DELETE FROM evedata.assets WHERE characterID = ?;", assets.TokenCharacterID)
	if err != nil {
		log.Println(err)
		return err
	}

	// early out if there are no assets.
	if len(assets.Assets) == 0 {
		return tx.Commit()
	}

	// Dump all assets into the DB.
	assetSQL := sq.Insert("evedata.assets").<commit_msg>retry this query<commit_after>package nail

import (
	"log"

	"github.com/antihax/evedata/internal/sqlhelper"

	sq "github.com/Masterminds/squirrel"
	"github.com/antihax/evedata/internal/datapackages"
	"github.com/antihax/evedata/internal/gobcoder"
	nsq "github.com/nsqio/go-nsq"
)

func init() {
	AddHandler("characterAssets", func(s *Nail, consumer *nsq.Consumer) {
		consumer.AddConcurrentHandlers(s.wait(nsq.HandlerFunc(s.characterAssetsConsumer)), 5)
	})
}

func (s *Nail) characterAssetsConsumer(message *nsq.Message) error {
	assets := datapackages.CharacterAssets{}
	err := gobcoder.GobDecoder(message.Body, &assets)
	if err != nil {
		log.Println(err)
		return err
	}

	// Start a new transaction to keep the delete an inserts together
	tx, err := s.db.Beginx()
	if err != nil {
		log.Println(err)
		return err
	}
	defer tx.Rollback()

	// Delete everything because we will replace with the new assets.
	_, err = tx.Exec("DELETE FROM evedata.assets WHERE characterID = ?;", assets.TokenCharacterID)
	if err != nil {
		log.Println(err)
		return err
	}

	// early out if there are no assets.
	if len(assets.Assets) == 0 {
		return sqlhelper.RetryTransaction(tx)
	}

	// Dump all assets into the DB.
	assetSQL := sq.Insert("evedata.assets").
{'fraction_non_alphanumeric': 0.13106796116504854, 'fraction_numerical': 0.0016181229773462784, 'mean_word_length': 4.141372141372141, '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': '1422542', 'n_tokens_mistral': 933, 'n_tokens_neox': 899, 'n_words': 205}