From 0dc12c9e3df35e4ede83c33c4d744d2f459b398d Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Tue, 28 Jun 2016 17:04:08 -0700 Subject: [PATCH] HRS: Show the size of containers (#1925) Fixes #1837 --- cmd/noms-log/noms_log_test.go | 8 ++--- go/types/encode_human_readable.go | 35 +++++++++++++++++++-- go/types/encode_human_readable_test.go | 42 ++++++++++++++++++++------ 3 files changed, 68 insertions(+), 17 deletions(-) diff --git a/cmd/noms-log/noms_log_test.go b/cmd/noms-log/noms_log_test.go index a8e2fc0d53..230fc51158 100644 --- a/cmd/noms-log/noms_log_test.go +++ b/cmd/noms-log/noms_log_test.go @@ -312,11 +312,11 @@ const ( graphRes3 = "* sha1-a859b0936cc42f073c03a27eb820f29a57f025f3\n|\\ Merge: sha1-d996bef5350a0316da794ae1f262dfcdcd4b2e8c sha1-65c99ad8e4db74b2cb3bf00bff806a4d46a220eb\n| | \"2222-wz\"\n| | \n* | sha1-d996bef5350a0316da794ae1f262dfcdcd4b2e8c\n|\\ \\ Merge: sha1-da122844f28f7a814fb99a4950ff673ea7e3611c sha1-ba3e907b42eaba15bf01752b012c92f8e5821536\n| | | \"222-wy\"\n| | | \n| * | sha1-ba3e907b42eaba15bf01752b012c92f8e5821536\n| |\\ \\ Merge: sha1-8af6c17c296c36f2b5adf19b02a83918a61088d1 sha1-624532f5d8a5839344ccfc465957a975e1962b6d\n| | | | \"22-wx\"\n| | | | \n* | | | sha1-da122844f28f7a814fb99a4950ff673ea7e3611c\n| | | | Parent: sha1-624532f5d8a5839344ccfc465957a975e1962b6d\n| | | | \"200-y\"\n| | | | \n| * | | sha1-8af6c17c296c36f2b5adf19b02a83918a61088d1\n| | | | Parent: sha1-624532f5d8a5839344ccfc465957a975e1962b6d\n| | | | \"20-x\"\n| | | | \n| | | * sha1-65c99ad8e4db74b2cb3bf00bff806a4d46a220eb\n|/ / / Parent: sha1-624532f5d8a5839344ccfc465957a975e1962b6d\n| \"2000-z\"\n| \n* sha1-624532f5d8a5839344ccfc465957a975e1962b6d\n| Parent: sha1-611d5d868352d4d6ae9b778d6627b81f769cdef5\n| \"2\"\n| \n* sha1-611d5d868352d4d6ae9b778d6627b81f769cdef5\n| Parent: None\n| \"1\"\n" diffRes3 = "* sha1-a859b0936cc42f073c03a27eb820f29a57f025f3\n|\\ Merge: sha1-d996bef5350a0316da794ae1f262dfcdcd4b2e8c sha1-65c99ad8e4db74b2cb3bf00bff806a4d46a220eb\n| | - \"222-wy\"\n| | + \"2222-wz\"\n| | \n* | sha1-d996bef5350a0316da794ae1f262dfcdcd4b2e8c\n|\\ \\ Merge: sha1-da122844f28f7a814fb99a4950ff673ea7e3611c sha1-ba3e907b42eaba15bf01752b012c92f8e5821536\n| | | - \"200-y\"\n| | | + \"222-wy\"\n| | | \n| * | sha1-ba3e907b42eaba15bf01752b012c92f8e5821536\n| |\\ \\ Merge: sha1-8af6c17c296c36f2b5adf19b02a83918a61088d1 sha1-624532f5d8a5839344ccfc465957a975e1962b6d\n| | | | - \"20-x\"\n| | | | + \"22-wx\"\n| | | | \n* | | | sha1-da122844f28f7a814fb99a4950ff673ea7e3611c\n| | | | Parent: sha1-624532f5d8a5839344ccfc465957a975e1962b6d\n| | | | - \"2\"\n| | | | + \"200-y\"\n| | | | \n| * | | sha1-8af6c17c296c36f2b5adf19b02a83918a61088d1\n| | | | Parent: sha1-624532f5d8a5839344ccfc465957a975e1962b6d\n| | | | - \"2\"\n| | | | + \"20-x\"\n| | | | \n| | | * sha1-65c99ad8e4db74b2cb3bf00bff806a4d46a220eb\n|/ / / Parent: sha1-624532f5d8a5839344ccfc465957a975e1962b6d\n| - \"2\"\n| + \"2000-z\"\n| \n* sha1-624532f5d8a5839344ccfc465957a975e1962b6d\n| Parent: sha1-611d5d868352d4d6ae9b778d6627b81f769cdef5\n| - \"1\"\n| + \"2\"\n| \n* sha1-611d5d868352d4d6ae9b778d6627b81f769cdef5\n| Parent: None\n| \n" - truncRes1 = "* sha1-39d1f600887364b2e4832fe80f6853b0966a9e6c\n| Parent: sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| List([\n| \"one\",\n| \"two\",\n| \"three\",\n| \"four\",\n| \"five\",\n| \"six\",\n| \"seven\",\n| ...\n| \n* sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| Parent: None\n| \"the first line\"\n" - diffTrunc1 = "* sha1-39d1f600887364b2e4832fe80f6853b0966a9e6c\n| Parent: sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| - \"the first line\"\n| + [\n| + \"one\",\n| + \"two\",\n| + \"three\",\n| + \"four\",\n| + \"five\",\n| + \"six\",\n| ...\n| \n* sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| Parent: None\n| \n" + truncRes1 = "* sha1-39d1f600887364b2e4832fe80f6853b0966a9e6c\n| Parent: sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| List([ // 11 items\n| \"one\",\n| \"two\",\n| \"three\",\n| \"four\",\n| \"five\",\n| \"six\",\n| \"seven\",\n| ...\n| \n* sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| Parent: None\n| \"the first line\"\n" + diffTrunc1 = "* sha1-39d1f600887364b2e4832fe80f6853b0966a9e6c\n| Parent: sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| - \"the first line\"\n| + [ // 11 items\n| + \"one\",\n| + \"two\",\n| + \"three\",\n| + \"four\",\n| + \"five\",\n| + \"six\",\n| ...\n| \n* sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| Parent: None\n| \n" - truncRes2 = "* sha1-39d1f600887364b2e4832fe80f6853b0966a9e6c\n| Parent: sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| List([\n| \"one\",\n| \"two\",\n| \"three\",\n| \"four\",\n| \"five\",\n| \"six\",\n| \"seven\",\n| \"eight\",\n| \"nine\",\n| \"ten\",\n| \"eleven\",\n| ])\n| \n* sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| Parent: None\n| \"the first line\"\n" - diffTrunc2 = "* sha1-39d1f600887364b2e4832fe80f6853b0966a9e6c\n| Parent: sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| - \"the first line\"\n| + [\n| + \"one\",\n| + \"two\",\n| + \"three\",\n| + \"four\",\n| + \"five\",\n| + \"six\",\n| + \"seven\",\n| + \"eight\",\n| + \"nine\",\n| + \"ten\",\n| + \"eleven\",\n| + ]\n| \n* sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| Parent: None\n| \n" + truncRes2 = "* sha1-39d1f600887364b2e4832fe80f6853b0966a9e6c\n| Parent: sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| List([ // 11 items\n| \"one\",\n| \"two\",\n| \"three\",\n| \"four\",\n| \"five\",\n| \"six\",\n| \"seven\",\n| \"eight\",\n| \"nine\",\n| \"ten\",\n| \"eleven\",\n| ])\n| \n* sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| Parent: None\n| \"the first line\"\n" + diffTrunc2 = "* sha1-39d1f600887364b2e4832fe80f6853b0966a9e6c\n| Parent: sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| - \"the first line\"\n| + [ // 11 items\n| + \"one\",\n| + \"two\",\n| + \"three\",\n| + \"four\",\n| + \"five\",\n| + \"six\",\n| + \"seven\",\n| + \"eight\",\n| + \"nine\",\n| + \"ten\",\n| + \"eleven\",\n| + ]\n| \n* sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| Parent: None\n| \n" truncRes3 = "* sha1-39d1f600887364b2e4832fe80f6853b0966a9e6c\n| Parent: sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n* sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| Parent: None\n" diffTrunc3 = "* sha1-39d1f600887364b2e4832fe80f6853b0966a9e6c\n| Parent: sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n* sha1-185ad8e966cba1a70b7f6cf19cd7bc5a7983c3d2\n| Parent: None\n" diff --git a/go/types/encode_human_readable.go b/go/types/encode_human_readable.go index 2eef4ecd3a..14fa33f766 100644 --- a/go/types/encode_human_readable.go +++ b/go/types/encode_human_readable.go @@ -11,6 +11,7 @@ import ( "strconv" "github.com/attic-labs/noms/go/d" + humanize "github.com/dustin/go-humanize" ) // Human Readable Serialization @@ -56,13 +57,20 @@ func (w *hrsWriter) newLine() { // hexWriter is used to write blob byte data as "00 01 ... 0f\n10 11 .." // hexWriter is an io.Writer that writes to an underlying hrsWriter. type hexWriter struct { - hrs *hrsWriter - count uint + hrs *hrsWriter + count uint + sizeWritten bool + size uint64 } func (w *hexWriter) Write(p []byte) (n int, err error) { for _, v := range p { if w.count == 16 { + if !w.sizeWritten { + w.hrs.write(" // ") + w.hrs.write(humanize.Bytes(w.size)) + w.sizeWritten = true + } w.hrs.newLine() w.count = 0 } else if w.count != 0 { @@ -91,11 +99,12 @@ func (w *hrsWriter) Write(v Value) { case BlobKind: w.maybeWriteIndentation() blob := v.(Blob) - encoder := &hexWriter{hrs: w} + encoder := &hexWriter{hrs: w, size: blob.Len()} _, w.err = io.Copy(encoder, blob.Reader()) case ListKind: w.write("[") + w.writeSize(v) w.indent() v.(List).Iter(func(v Value, i uint64) bool { if i == 0 { @@ -111,6 +120,7 @@ func (w *hrsWriter) Write(v Value) { case MapKind: w.write("{") + w.writeSize(v) w.indent() first := true v.(Map).Iter(func(key, val Value) bool { @@ -133,6 +143,7 @@ func (w *hrsWriter) Write(v Value) { case SetKind: w.write("{") + w.writeSize(v) w.indent() first := true v.(Set).Iter(func(v Value) bool { @@ -209,6 +220,24 @@ func (w *hrsWriter) WriteTagged(v Value) { } } +type lenable interface { + Len() uint64 +} + +func (w *hrsWriter) writeSize(v Value) { + t := v.Type() + switch t.Kind() { + case ListKind, MapKind, SetKind: + l := v.(lenable).Len() + if l < 4 { + return + } + w.write(fmt.Sprintf(" // %s items", humanize.Comma(int64(l)))) + default: + panic("unreachable") + } +} + func (w *hrsWriter) writeType(t *Type, parentStructTypes []*Type) { switch t.Kind() { case BlobKind, BoolKind, NumberKind, StringKind, TypeKind, ValueKind: diff --git a/go/types/encode_human_readable_test.go b/go/types/encode_human_readable_test.go index 679dc81976..6b078adb80 100644 --- a/go/types/encode_human_readable_test.go +++ b/go/types/encode_human_readable_test.go @@ -7,6 +7,7 @@ package types import ( "bytes" "errors" + "strings" "testing" "github.com/attic-labs/testify/assert" @@ -68,16 +69,31 @@ func TestWriteHumanReadableRef(t *testing.T) { func TestWriteHumanReadableCollections(t *testing.T) { l := NewList(Number(0), Number(1), Number(2), Number(3)) - assertWriteHRSEqual(t, "[\n 0,\n 1,\n 2,\n 3,\n]", l) - assertWriteTaggedHRSEqual(t, "List([\n 0,\n 1,\n 2,\n 3,\n])", l) + assertWriteHRSEqual(t, "[ // 4 items\n 0,\n 1,\n 2,\n 3,\n]", l) + assertWriteTaggedHRSEqual(t, "List([ // 4 items\n 0,\n 1,\n 2,\n 3,\n])", l) s := NewSet(Number(0), Number(1), Number(2), Number(3)) - assertWriteHRSEqual(t, "{\n 0,\n 1,\n 2,\n 3,\n}", s) - assertWriteTaggedHRSEqual(t, "Set({\n 0,\n 1,\n 2,\n 3,\n})", s) + assertWriteHRSEqual(t, "{ // 4 items\n 0,\n 1,\n 2,\n 3,\n}", s) + assertWriteTaggedHRSEqual(t, "Set({ // 4 items\n 0,\n 1,\n 2,\n 3,\n})", s) m := NewMap(Number(0), Bool(false), Number(1), Bool(true)) assertWriteHRSEqual(t, "{\n 0: false,\n 1: true,\n}", m) assertWriteTaggedHRSEqual(t, "Map({\n 0: false,\n 1: true,\n})", m) + + l2 := NewList() + assertWriteHRSEqual(t, "[]", l2) + assertWriteTaggedHRSEqual(t, "List<>([])", l2) + + l3 := NewList(Number(0)) + assertWriteHRSEqual(t, "[\n 0,\n]", l3) + assertWriteTaggedHRSEqual(t, "List([\n 0,\n])", l3) + + nums := make([]Value, 2000) + for i := range nums { + nums[i] = Number(0) + } + l4 := NewList(nums...) + assertWriteTaggedHRSEqual(t, "List([ // 2,000 items\n"+strings.Repeat(" 0,\n", 2000)+"])", l4) } func TestWriteHumanReadableNested(t *testing.T) { @@ -192,16 +208,22 @@ func TestWriteHumanReadableBlob(t *testing.T) { 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, })) - assertWriteHRSEqual(t, "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n10", b4) - assertWriteTaggedHRSEqual(t, "Blob(00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n10)", b4) + assertWriteHRSEqual(t, "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f // 17 B\n10", b4) + assertWriteTaggedHRSEqual(t, "Blob(00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f // 17 B\n10)", b4) bs := make([]byte, 256) for i := range bs { bs[i] = byte(i) } + b5 := NewBlob(bytes.NewBuffer(bs)) - assertWriteHRSEqual(t, "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f\n20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f\n30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f\n40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f\n50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f\n60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f\n70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f\n80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f\n90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f\na0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af\nb0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf\nc0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf\nd0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df\ne0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef\nf0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff", b5) - assertWriteTaggedHRSEqual(t, "Blob(00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f\n20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f\n30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f\n40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f\n50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f\n60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f\n70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f\n80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f\n90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f\na0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af\nb0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf\nc0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf\nd0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df\ne0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef\nf0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff)", b5) + assertWriteHRSEqual(t, "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f // 256 B\n10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f\n20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f\n30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f\n40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f\n50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f\n60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f\n70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f\n80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f\n90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f\na0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af\nb0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf\nc0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf\nd0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df\ne0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef\nf0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff", b5) + assertWriteTaggedHRSEqual(t, "Blob(00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f // 256 B\n10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f\n20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f\n30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f\n40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f\n50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f\n60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f\n70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f\n80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f\n90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f\na0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af\nb0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf\nc0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf\nd0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df\ne0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef\nf0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff)", b5) + + b6 := NewBlob(bytes.NewBuffer(make([]byte, 16*100))) + row := strings.Repeat("00 ", 15) + "00" + s := strings.Repeat(row+"\n", 98) + row + assertWriteTaggedHRSEqual(t, "Blob("+row+" // 1.6 kB\n"+s+")", b6) } func TestWriteHumanReadableListOfBlob(t *testing.T) { @@ -213,8 +235,8 @@ func TestWriteHumanReadableListOfBlob(t *testing.T) { 0x10, })) l := NewList(b1, NewEmptyBlob(), b2, b3) - assertWriteHRSEqual(t, "[\n 01,\n ,\n 02,\n 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n 10,\n]", l) - assertWriteTaggedHRSEqual(t, "List([\n 01,\n ,\n 02,\n 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n 10,\n])", l) + assertWriteHRSEqual(t, "[ // 4 items\n 01,\n ,\n 02,\n 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f // 17 B\n 10,\n]", l) + assertWriteTaggedHRSEqual(t, "List([ // 4 items\n 01,\n ,\n 02,\n 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f // 17 B\n 10,\n])", l) } func TestWriteHumanReadableType(t *testing.T) {