11package store
22
33import (
4- "encoding/json"
54 "fmt"
65 "log"
7- "os"
8- "path/filepath"
96 "strings"
107)
118
12- // Item represents one string entry exported from ixion (per sheet/row).
13- type Item struct {
14- Sheet string `json:"sheet"`
15- RowID string `json:"rowId"`
16- Values map [string ]string `json:"values"`
17- Index int `json:"index"`
18- }
19-
209// Store keeps all items in memory and provides simple lookup helpers.
2110type Store struct {
2211 items []* Item
@@ -31,68 +20,43 @@ func LoadStore(dataDir string) (*Store, error) {
3120 sheetIndex : make (map [string ]int ),
3221 }
3322
34- var files []string
35- err := filepath .Walk (dataDir , func (path string , info os.FileInfo , err error ) error {
36- if err != nil {
37- return err
38- }
39- if ! info .IsDir () && strings .HasSuffix (info .Name (), ".json" ) {
40- files = append (files , path )
41- }
42- return nil
43- })
23+ files , err := scanDataFiles (dataDir )
4424 if err != nil {
45- return nil , fmt .Errorf ("walk data files: %w" , err )
25+ return nil , fmt .Errorf ("scan data files: %w" , err )
4626 }
4727 if len (files ) == 0 {
4828 log .Printf ("no data files found in %s" , dataDir )
4929 }
5030
5131 for _ , path := range files {
52- if err := s .loadFile (path ); err != nil {
53- return nil , err
32+ items , err := loadFile (path )
33+ if err != nil {
34+ return nil , fmt .Errorf ("load file %s: %w" , path , err )
35+ }
36+ for _ , it := range items {
37+ s .addItem (it )
5438 }
5539 }
5640
5741 log .Printf ("loaded %d items from %d files" , len (s .items ), len (files ))
5842 return s , nil
5943}
6044
61- func (s * Store ) loadFile (path string ) error {
62- f , err := os .Open (path )
63- if err != nil {
64- return fmt .Errorf ("open %s: %w" , path , err )
65- }
66- defer f .Close ()
67-
68- decoder := json .NewDecoder (f )
69- var arr []* Item
70- if err := decoder .Decode (& arr ); err != nil {
71- return fmt .Errorf ("decode %s: %w" , path , err )
45+ func (s * Store ) addItem (it * Item ) {
46+ // Assign index based on sheet and order in file
47+ // Index is unique per sheet and follows the order items appear in files
48+ if _ , ok := s .sheetIndex [it .Sheet ]; ! ok {
49+ s .sheetIndex [it .Sheet ] = 0
7250 }
51+ it .Index = s .sheetIndex [it .Sheet ]
52+ s .sheetIndex [it .Sheet ]++
7353
74- for _ , it := range arr {
75- if it == nil {
76- continue
77- }
54+ s .items = append (s .items , it )
7855
79- // Assign index based on sheet and order in file
80- // Index is unique per sheet and follows the order items appear in files
81- if _ , ok := s .sheetIndex [it .Sheet ]; ! ok {
82- s .sheetIndex [it .Sheet ] = 0
83- }
84- it .Index = s .sheetIndex [it .Sheet ]
85- s .sheetIndex [it .Sheet ]++
86-
87- s .items = append (s .items , it )
88-
89- if _ , ok := s .bySheet [it .Sheet ]; ! ok {
90- s .bySheet [it .Sheet ] = make ([]* Item , 0 )
91- }
92- s .bySheet [it .Sheet ] = append (s .bySheet [it .Sheet ], it )
56+ if _ , ok := s .bySheet [it .Sheet ]; ! ok {
57+ s .bySheet [it .Sheet ] = make ([]* Item , 0 )
9358 }
94-
95- return nil
59+ s .bySheet [it .Sheet ] = append (s .bySheet [it .Sheet ], it )
9660}
9761
9862// Search finds items whose value in the given language contains the query substring.
0 commit comments