Skip to content

Commit

Permalink
Deprecate withContent=false and names_tar
Browse files Browse the repository at this point in the history
  • Loading branch information
angelini committed Oct 14, 2022
1 parent a3927c5 commit fe035ea
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 346 deletions.
4 changes: 1 addition & 3 deletions development/scripts/load-tests/get_all.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[
{
"project": "{{ randomInt 0 9 }}{{ randomInt 0 9 }}",
"queries": [
{"path": "", "is_prefix": false, "with_content": true}
]
"queries": [{ "path": "", "is_prefix": false }]
}
]
5 changes: 2 additions & 3 deletions internal/db/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ func CopyAllObjects(ctx context.Context, tx pgx.Tx, source int64, target int64)

func CloneToProject(ctx context.Context, tx pgx.Tx, source int64, target int64, vrange VersionRange, newTargetVersion int64) error {
objectQuery := &pb.ObjectQuery{
Path: "",
IsPrefix: true,
WithContent: false,
Path: "",
IsPrefix: true,
}

builder := newQueryBuilder(source, vrange, objectQuery).withHashes(true)
Expand Down
2 changes: 1 addition & 1 deletion internal/db/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func GetTars(ctx context.Context, tx pgx.Tx, project int64, cacheVersions []int6
object = NewUncachedTarObject(path, mode, size, deleted, content)
}

err = tarWriter.WriteObject(&object, true)
err = tarWriter.WriteObject(&object)
if err != nil {
return nil, nil, err
}
Expand Down
13 changes: 2 additions & 11 deletions internal/db/queryBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func (qb *queryBuilder) updatedObjectsCTE() string {
FROM possible_objects o
LEFT JOIN dl.contents c
ON o.hash = c.hash
%s
%s
WHERE o.project = __project__
AND o.start_version > __start_version__
Expand All @@ -86,16 +85,8 @@ func (qb *queryBuilder) updatedObjectsCTE() string {
ORDER BY o.path
`

bytesSelector := "c.bytes"
contentsPredicate := ""
if !qb.objectQuery.WithContent {
bytesSelector = "c.names_tar AS bytes"
contentsPredicate = "AND o.packed IS true"
}

// FIXME: We do not support cacheVersions and !withContent at the same time

isCachedSelector := "false AS is_cached"
bytesSelector := "c.bytes"
cacheJoin := ""
if len(qb.cacheVersions) > 0 {
isCachedSelector = "h.hash IS NOT NULL AS is_cached"
Expand Down Expand Up @@ -123,7 +114,7 @@ func (qb *queryBuilder) updatedObjectsCTE() string {
ignoresPredicate = "AND o.path NOT LIKE ALL(__ignores__::text[])"
}

return fmt.Sprintf(template, isCachedSelector, bytesSelector, hashSelector, contentsPredicate, cacheJoin, pathPredicate, ignoresPredicate)
return fmt.Sprintf(template, isCachedSelector, bytesSelector, hashSelector, cacheJoin, pathPredicate, ignoresPredicate)
}

func (qb *queryBuilder) removedObjectsCTE() string {
Expand Down
31 changes: 10 additions & 21 deletions internal/db/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func (t *TarWriter) Size() int {
return t.size
}

func (t *TarWriter) WriteObject(object *TarObject, writeContent bool) error {
func (t *TarWriter) WriteObject(object *TarObject) error {
typeFlag := object.TarType()

size := int64(len(object.content))
if !writeContent || typeFlag == tar.TypeDir || typeFlag == tar.TypeSymlink {
if typeFlag == tar.TypeDir || typeFlag == tar.TypeSymlink {
size = 0
}

Expand Down Expand Up @@ -178,9 +178,8 @@ func (t *TarReader) CopyContent(buffer io.Writer) error {
return err
}

func PackObjects(objects ObjectStream) ([]byte, []byte, error) {
func PackObjects(objects ObjectStream) ([]byte, error) {
contentWriter := NewTarWriter()
namesWriter := NewTarWriter()
empty := true

for {
Expand All @@ -192,41 +191,31 @@ func PackObjects(objects ObjectStream) ([]byte, []byte, error) {
break
}
if err != nil {
return nil, nil, err
return nil, err
}

empty = false

tarObj := NewUncachedTarObject(object.Path, object.Mode, object.Size, object.Deleted, object.Content)
err = contentWriter.WriteObject(&tarObj, true)
if err != nil {
return nil, nil, err
}

err = namesWriter.WriteObject(&tarObj, false)
err = contentWriter.WriteObject(&tarObj)
if err != nil {
return nil, nil, err
return nil, err
}
}

if empty {
return nil, nil, ErrEmptyPack
return nil, ErrEmptyPack
}

contentTar, err := contentWriter.BytesAndReset()
if err != nil {
return nil, nil, err
}

namesTar, err := namesWriter.BytesAndReset()
if err != nil {
return nil, nil, err
return nil, err
}

return contentTar, namesTar, nil
return contentTar, nil
}

func updateObjects(before []byte, updates []*pb.Object) ([]byte, []byte, error) {
func updateObjects(before []byte, updates []*pb.Object) ([]byte, error) {
seenPaths := make(map[string]bool)
idxHint := 0

Expand Down
14 changes: 7 additions & 7 deletions internal/db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func UpdateObject(ctx context.Context, tx pgx.Tx, encoder *ContentEncoder, proje
`, version, project, previousPaths, version)

batch.Queue(`
INSERT INTO dl.contents (hash, bytes, names_tar)
VALUES (($1, $2), $3, NULL)
INSERT INTO dl.contents (hash, bytes)
VALUES (($1, $2), $3)
ON CONFLICT
DO NOTHING
`, hash.H1, hash.H2, encoded)
Expand Down Expand Up @@ -154,7 +154,7 @@ func UpdatePackedObjects(ctx context.Context, tx pgx.Tx, project int64, version
rows.Close()

shouldInsert := true
updated, namesTar, err := updateObjects(content, updates)
updated, err := updateObjects(content, updates)
if errors.Is(err, ErrEmptyPack) {
// If the newly packed object is empty, we only need to delete the old one.
shouldInsert = false
Expand Down Expand Up @@ -186,11 +186,11 @@ func UpdatePackedObjects(ctx context.Context, tx pgx.Tx, project int64, version
`, project, version, parent, newHash.H1, newHash.H2, 0, len(updated), true)

batch.Queue(`
INSERT INTO dl.contents (hash, bytes, names_tar)
VALUES (($1, $2), $3, $4)
INSERT INTO dl.contents (hash, bytes)
VALUES (($1, $2), $3)
ON CONFLICT
DO NOTHING
`, newHash.H1, newHash.H2, updated, namesTar)
DO NOTHING
`, newHash.H1, newHash.H2, updated)
}

results := tx.SendBatch(ctx, batch)
Expand Down
1 change: 0 additions & 1 deletion internal/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const (
QueryIgnores = StringSliceKey("dl.query.ignores")
QueryIsPrefix = BoolKey("dl.query.is_prefix")
QueryPath = StringKey("dl.query.path")
QueryWithContent = BoolKey("dl.query.with_content")
SampleRate = Float32Key("dl.sample_rate")
Server = StringKey("dl.server")
State = StringKey("dl.state")
Expand Down
Loading

0 comments on commit fe035ea

Please sign in to comment.