diff --git a/mount.go b/mount.go index 2c238dc..93ebdb0 100644 --- a/mount.go +++ b/mount.go @@ -123,12 +123,25 @@ func buildMountTree(ctx context.Context, client *DocspellClient, items []SearchI byIDName = fmt.Sprintf("%s-%d%s", item.ID, i+1, ext) } } - byID.children[uniqueName(byID.children, sanitizeName(byIDName))] = node + shard := idShard(item.ID) + shardDir := ensureDir(byID, shard) + shardDir.children[uniqueName(shardDir.children, sanitizeName(byIDName))] = node } } return root } +func idShard(id string) string { + id = sanitizeName(id) + if len(id) >= 2 { + return id[:2] + } + if id != "" { + return id + } + return "__" +} + func ensureDir(root *dirNode, first string, rest ...string) *dirNode { cur := root parts := append([]string{first}, rest...) diff --git a/mount_test.go b/mount_test.go index f4480b4..e927c4f 100644 --- a/mount_test.go +++ b/mount_test.go @@ -87,9 +87,13 @@ func TestBuildMountTreeAddsByIDDirectory(t *testing.T) { if !ok { t.Fatalf("expected by-id directory, got %#v", root.children) } + shard, ok := byID.children["it"].(*dirNode) + if !ok { + t.Fatalf("expected by-id shard, got %#v", byID.children) + } for _, name := range []string{"item-1.pdf", "item-1-2.txt", "item-1-3.pdf"} { - if _, ok := byID.children[name].(*fileNode); !ok { - t.Fatalf("expected %s in by-id, got %#v", name, byID.children) + if _, ok := shard.children[name].(*fileNode); !ok { + t.Fatalf("expected %s in by-id shard, got %#v", name, shard.children) } } }