able to parse xml

This commit is contained in:
2024-01-28 16:49:48 +03:00
parent 90a7797a27
commit bfa105df95
12 changed files with 298 additions and 89 deletions

View File

@ -42,7 +42,7 @@ func (c *goodsItemClient) prefixedStr(key string) []byte {
return c.prefixed(keyBytes)
}
func (c *goodsItemClient) prefixedIDByCartStr(key int64) []byte {
func (c *goodsItemClient) prefixedIDByCartInt64(key int64) []byte {
var keyBytes [8]byte
binary.BigEndian.PutUint64(keyBytes[:], uint64(key))
return c.prefixedIDByCart(keyBytes[:])
@ -168,22 +168,21 @@ func (c *goodsItemClient) Get(
func (c *goodsItemClient) GetByCart(ctx context.Context, id int64) (out entity.GoodsItem, err error) {
err = c.db.View(func(txn *badger.Txn) error {
var idByte [8]byte
binary.BigEndian.PutUint64(idByte[:], uint64(id))
item, err := txn.Get(c.prefixedIDByCart(idByte[:]))
idxKey := c.prefixedIDByCartInt64(id)
skuByCartIDItem, err := txn.Get(idxKey)
if err != nil {
return fmt.Errorf("getting key: %w", err)
}
sku := make([]byte, item.ValueSize())
sku, err = item.ValueCopy(sku)
sku := make([]byte, skuByCartIDItem.ValueSize())
sku, err = skuByCartIDItem.ValueCopy(sku)
if err != nil {
return fmt.Errorf("getting value of idx: %w", err)
}
// well, yeah, that's kind of dumb to trim prefix here and
// and prefix later, but who cares.
sku = bytes.TrimPrefix(sku, c.prefix())
out, err = c.getBySKU(sku, txn)
return err
})
@ -206,40 +205,44 @@ func (c *goodsItemClient) upsertByBatch(ctx context.Context, items []entity.Good
batch := c.db.NewWriteBatch()
defer batch.Cancel()
log := zerolog.Ctx(ctx)
err := func() error {
for _, item := range items {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
for _, item := range items {
select {
case <-ctx.Done():
break
default:
}
key := c.prefixedStr(item.Articul)
var value []byte
if useJSON {
value, _ = json.Marshal(item)
} else {
value = fbs.MakeDomainGoodItemFinished(item)
key := c.prefixedStr(item.Articul)
var value []byte
if useJSON {
value, _ = json.Marshal(item)
} else {
value = fbs.MakeDomainGoodItemFinished(item)
}
idxValue := make([]byte, len(key))
copy(idxValue, key)
coreEntry := badger.NewEntry(key, value)
if err := batch.SetEntry(coreEntry); err != nil {
return fmt.Errorf("setting core entry: %w", err)
}
idxKey := c.prefixedIDByCartInt64(item.Cart)
idxEntry := badger.NewEntry(idxKey, idxValue)
if err := batch.SetEntry(idxEntry); err != nil {
return fmt.Errorf("setting index entry: %w", err)
}
}
idxValue := make([]byte, len(key))
copy(idxValue, key)
coreEntry := badger.NewEntry(key, value)
if err := batch.SetEntry(coreEntry); err != nil {
log.Warn().Err(err).Msg("unable to set item, breaking")
break
}
idxKey := c.prefixedIDByCartStr(item.Cart)
idxEntry := badger.NewEntry(idxKey, idxValue)
if err := batch.SetEntry(idxEntry); err != nil {
log.Warn().Err(err).Msg("unable to set idx, breaking")
break
}
return nil
}()
if err != nil && !errors.Is(err, context.Canceled) {
return err
}
err := batch.Flush()
err = batch.Flush()
if err != nil {
return fmt.Errorf("flushing changes: %w", err)
}