@@ -22,8 +22,9 @@ type Collection struct {
2222 rowsMutex * sync.Mutex
2323 Indexes map [string ]* collectionIndex // todo: protect access with mutex or use sync.Map
2424 // buffer *bufio.Writer // TODO: use write buffer to improve performance (x3 in tests)
25- Defaults map [string ]any
26- Count int64
25+ Defaults map [string ]any
26+ Count int64
27+ jsonEncoder * json.Encoder
2728}
2829
2930type collectionIndex struct {
@@ -136,6 +137,8 @@ func OpenCollection(filename string) (*Collection, error) {
136137 return nil , fmt .Errorf ("open file for write: %w" , err )
137138 }
138139
140+ collection .jsonEncoder = json .NewEncoder (collection .file )
141+
139142 return collection , nil
140143}
141144
@@ -212,9 +215,9 @@ func (c *Collection) Insert(item map[string]any) (*Row, error) {
212215 Payload : payload ,
213216 }
214217
215- err = json . NewEncoder ( c . file ). Encode (command )
218+ err = c . EncodeCommand (command )
216219 if err != nil {
217- return nil , fmt . Errorf ( "json encode command: %w" , err )
220+ return nil , err
218221 }
219222
220223 return row , nil
@@ -283,12 +286,7 @@ func (c *Collection) setDefaults(defaults map[string]any, persist bool) error {
283286 Payload : payload ,
284287 }
285288
286- err = json .NewEncoder (c .file ).Encode (command )
287- if err != nil {
288- return fmt .Errorf ("json encode command: %w" , err )
289- }
290-
291- return nil
289+ return c .EncodeCommand (command )
292290}
293291
294292// IndexMap create a unique index with a name
@@ -350,12 +348,7 @@ func (c *Collection) createIndex(name string, options interface{}, persist bool)
350348 Payload : payload ,
351349 }
352350
353- err = json .NewEncoder (c .file ).Encode (command )
354- if err != nil {
355- return fmt .Errorf ("json encode command: %w" , err )
356- }
357-
358- return nil
351+ return c .EncodeCommand (command )
359352}
360353
361354func indexInsert (indexes map [string ]* collectionIndex , row * Row ) (err error ) {
@@ -454,13 +447,7 @@ func (c *Collection) removeByRow(row *Row, persist bool) error { // todo: rename
454447 Payload : payload ,
455448 }
456449
457- err = json .NewEncoder (c .file ).Encode (command )
458- if err != nil {
459- // TODO: panic?
460- return fmt .Errorf ("json encode command: %w" , err )
461- }
462-
463- return nil
450+ return c .EncodeCommand (command )
464451}
465452
466453func (c * Collection ) Patch (row * Row , patch interface {}) error {
@@ -521,12 +508,7 @@ func (c *Collection) patchByRow(row *Row, patch interface{}, persist bool) error
521508 Payload : payload ,
522509 }
523510
524- err = json .NewEncoder (c .file ).Encode (command )
525- if err != nil {
526- return fmt .Errorf ("json encode command: %w" , err )
527- }
528-
529- return nil
511+ return c .EncodeCommand (command )
530512}
531513
532514func (c * Collection ) Close () error {
@@ -583,7 +565,11 @@ func (c *Collection) dropIndex(name string, persist bool) error {
583565 Payload : payload ,
584566 }
585567
586- err = json .NewEncoder (c .file ).Encode (command )
568+ return c .EncodeCommand (command )
569+ }
570+
571+ func (c * Collection ) EncodeCommand (command * Command ) error {
572+ err := c .jsonEncoder .Encode (command )
587573 if err != nil {
588574 return fmt .Errorf ("json encode command: %w" , err )
589575 }
0 commit comments