diff --git a/carstore/scylla.go b/carstore/scylla.go index 8f872c02..17598419 100644 --- a/carstore/scylla.go +++ b/carstore/scylla.go @@ -154,6 +154,8 @@ func (sqs *ScyllaStore) writeNewShard(ctx context.Context, root cid.Cid, rev str span.SetAttributes(attribute.Int("blocks", len(blks))) + batch := sqs.WriteSession.NewBatch(gocql.LoggedBatch) + for bcid, block := range blks { // build shard for output firehose nw, err := LdWrite(buf, bcid.Bytes(), block.RawData()) @@ -165,15 +167,17 @@ func (sqs *ScyllaStore) writeNewShard(ctx context.Context, root cid.Cid, rev str // TODO: scylla BATCH doesn't apply if the batch crosses partition keys; BUT, we may be able to send many blocks concurrently? dbcid := bcid.Bytes() blockbytes := block.RawData() - // we're relying on cql auto-prepare, no 'PreparedStatement' - err = sqs.WriteSession.Query( - `INSERT INTO blocks (uid, cid, rev, root, block) VALUES (?, ?, ?, ?, ?)`, - user, dbcid, rev, dbroot, blockbytes, - ).Idempotent(true).Exec() - if err != nil { - return nil, fmt.Errorf("(uid,cid) block store failed, %w", err) - } - sqs.log.Debug("put block", "uid", user, "cid", bcid, "size", len(blockbytes)) + + batch.Entries = append(batch.Entries, gocql.BatchEntry{ + Stmt: `INSERT INTO blocks (uid, cid, rev, root, block) VALUES (?, ?, ?, ?, ?)`, + Args: []interface{}{user, dbcid, rev, dbroot, blockbytes}, + Idempotent: true, + }) + } + + err = sqs.WriteSession.ExecuteBatch(batch) + if err != nil { + return nil, fmt.Errorf("failed to write batch uid=%d: %w", user, err) } shard := CarShard{