-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcollection.go
1297 lines (1242 loc) · 34 KB
/
collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package dataset includes the operations needed for processing collections of JSON documents and their attachments.
//
// Authors R. S. Doiel, <[email protected]> and Tom Morrel, <[email protected]>
//
// Copyright (c) 2022, Caltech
// All rights not granted herein are expressly reserved by Caltech.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package dataset
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"path/filepath"
"strings"
"time"
// Caltech Library packages
"github.com/caltechlibrary/models"
// 3rd Party packages
"gopkg.in/yaml.v3"
)
const (
//
// Store types
//
// PTSTORE describes the storage type using a pairtree
PTSTORE = "pairtree"
// SQLSTORE describes the SQL storage type
SQLSTORE = "sqlstore"
)
// Collection is the holds both operational metadata
// for collection level operations on collections of JSON objects.
// General metadata is stored in a codemeta.json file in the root
// directory along side the collection.json file.
type Collection struct {
// DatasetVersion of the collection
DatasetVersion string `json:"dataset,omitempty"`
// Name of collection
Name string `json:"name"`
// StoreType can be either "pairtree" (default or if attribute is
// omitted) or "sqlstore". If sqlstore the connection string, DSN URI,
// will determine the type of SQL database being accessed.
StoreType string `json:"storage_type,omitempty"`
// DsnURI holds protocol plus dsn string. The protocol can be
// "sqlite://", "mysql://" or "postgres://"and the dsn conforming to the Golang
// database/sql driver name in the database/sql package.
DsnURI string `json:"dsn_uri,omitempty"`
// Model holds the an experimental schema expressed in YAML
// used to validate objects in a collection. By default it is nil and not used
// but if a "model.yaml" file exists in the collection root directory it'll be loaded
// allowing possible varification of structure data.
Model *models.Model `json:"-"`
// Created
Created string `json:"created,omitempty"`
// Repaired
Repaired string `json:"repaired,omitempty"`
// PTStore the point to the pairtree implementation of storage
PTStore *PTStore `json:"-"`
// SQLStore points to a SQL database with JSON column support
SQLStore *SQLStore `json:"-"`
// Versioning holds the type of versioning implemented in the collection.
// It can be set to an empty string (the default) which means no versioning.
// It can be set to "patch" which means objects and attachments are versioned by
// a semver patch value (e.g. 0.0.X where X is incremented), "minor" where
// the semver minor value is incremented (e.g. e.g. 0.X.0 where X is incremented),
// or "major" where the semver major value is incremented (e.g. X.0.0 where X is
// incremented). Versioning affects storage of JSON objects and their attachments
// across the whole collection.
Versioning string `json:"versioning,omitempty"`
//
// Private varibles
//
// workPath holds the path the directory where the collection.json
// file is found.
workPath string `json:"-"`
}
//
// Public interface for dataset
//
// Open reads in a collection's operational metadata and returns
// a new collection structure and error value.
//
// ```
//
// var (
// c *dataset.Collection
// err error
// )
// c, err = dataset.Open("collection.ds")
// if err != nil {
// // ... handle error
// }
// defer c.Close()
//
// ```
func Open(name string) (*Collection, error) {
// NOTE: find the collection.json file then
// open the appropriate store.
src, err := ioutil.ReadFile(path.Join(name, "collection.json"))
if err != nil {
return nil, err
}
c := new(Collection)
if err := json.Unmarshal(src, &c); err != nil {
return nil, err
}
fullPath, err := filepath.Abs(name)
if err == nil {
c.workPath = fullPath
} else {
c.workPath = name
}
if c.DsnURI == "" {
c.DsnURI = os.Getenv("DATASET_DSN_URI")
}
switch c.StoreType {
case PTSTORE:
c.PTStore, err = PTStoreOpen(c.workPath, c.DsnURI)
switch c.Versioning {
case "major":
c.PTStore.SetVersioning(Major)
case "minor":
c.PTStore.SetVersioning(Minor)
case "patch":
c.PTStore.SetVersioning(Patch)
default:
c.PTStore.SetVersioning(None)
}
case SQLSTORE:
c.SQLStore, err = SQLStoreOpen(c.workPath, c.DsnURI)
switch c.Versioning {
case "major":
c.SQLStore.SetVersioning(Major)
case "minor":
c.SQLStore.SetVersioning(Minor)
case "patch":
c.SQLStore.SetVersioning(Patch)
default:
c.SQLStore.SetVersioning(None)
}
default:
return nil, fmt.Errorf("failed to open %s, %q storage type not supported", name, c.StoreType)
}
// FIXME: Now check if there is a models.yaml file in the collection's root folder.
if _, err := os.Stat(path.Join(name, "model.yaml")); err == nil {
src, err = ioutil.ReadFile(path.Join(name, "model.yaml"))
if err != nil {
return c, fmt.Errorf("failed to read %s in %s, %s", name, path.Join(name, "model.yaml"), err)
}
model, err := models.NewModel("model")
if err != nil {
return c, err
}
models.SetDefaultTypes(model)
if err := yaml.Unmarshal(src, model); err != nil {
return c, fmt.Errorf("failed to parse %s, %s", path.Join(name, "model.yaml"), err)
}
//FIXME: add check here.
if model != nil {
c.Model = model
}
}
return c, err
}
// Close closes a collection. For a pairtree that means flushing the
// keymap to disk. For a SQL store it means closing a database connection.
// Close is often called in conjunction with "defer" keyword.
//
// ```
//
// c, err := dataset.Open("my_collection.ds")
// if err != nil { /* .. handle error ... */ }
// /* do some stuff with the collection */
// defer func() {
// if err := c.Close(); err != nil {
// /* ... handle closing error ... */
// }
// }()
//
// ```
func (c *Collection) Close() error {
switch c.StoreType {
case PTSTORE:
if c.PTStore != nil {
return c.PTStore.Close()
}
case SQLSTORE:
if c.SQLStore != nil {
return c.SQLStore.Close()
}
}
return fmt.Errorf("%q not supported", c.StoreType)
}
// WorkPath returns the working path to the collection.
func (c *Collection) WorkPath() string {
return c.workPath
}
// SetVersioning sets the versioning on a collection. The version string
// can be "major", "minor", "patch". Any other value (e.g. "", "off", "none")
// will turn off versioning for the collection.
func (c *Collection) SetVersioning(versioning string) error {
switch versioning {
case "major":
c.Versioning = versioning
case "minor":
c.Versioning = versioning
case "patch":
c.Versioning = versioning
default:
c.Versioning = ""
}
colName := path.Join(c.workPath, "collection.json")
src, err := JSONMarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("cannot encode %q, %s", colName, err)
}
if err := ioutil.WriteFile(colName, src, 0660); err != nil {
return fmt.Errorf("failed to create %q %s", colName, err)
}
return nil
}
// initPTStore takes a *Collection and initializes a PTSTORE collection.
// For pairtrees this means create the directory structure and writing
// out the collection.json file, a skeleton codemeta.json and an empty
// keys.json file.
func (c *Collection) initPTStore() error {
now := time.Now()
today := now.Format(datestamp)
c.DatasetVersion = Version
c.Created = now.UTC().Format(timestampUTC)
// Split see if c.Name path exists
if _, err := os.Stat(c.Name); os.IsNotExist(err) {
// Create directoriess if needed
if err := os.MkdirAll(c.Name, 0770); err != nil {
return fmt.Errorf("cannot create collect %q, %s", c.Name, err)
}
} else {
return fmt.Errorf("%q already exists", c.Name)
}
fullName := c.Name
basename := path.Base(c.Name)
colName := path.Join(c.Name, "collection.json")
c.Name = basename
src, err := JSONMarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("cannot encode %q, %s", colName, err)
}
c.Name = fullName
if err := ioutil.WriteFile(colName, src, 0660); err != nil {
return fmt.Errorf("failed to create %q %s", colName, err)
}
// Create a default codemeta.json file in the directory
//today := time.Now().Format(datestamp)
m := map[string]string{
"{today}": today,
"{c_name}": path.Base(c.Name),
"{app_name}": path.Base(os.Args[0]),
"{version}": Version,
}
src = BytesProcessor(m, []byte(`{
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
"@type": "SoftwareSourceCode",
"dateCreated": "{today}",
"name": "{c_name}",
"version": "0.0.0",
"releaseNotes": "This is a {app_name} {version} collection",
"developmentStatus": "concept",
"softwareRequirements": [
"https://github.com/caltechlibrary/dataset"
]
}`))
cmName := path.Join(c.Name, "codemeta.json")
if err := ioutil.WriteFile(cmName, src, 0664); err != nil {
return fmt.Errorf("failed to create %q, %s", cmName, err)
}
// Create the pairtree root
ptName := path.Join(c.Name, "pairtree")
if os.MkdirAll(ptName, 0770); err != nil {
return fmt.Errorf("failed to create pairtree root, %s", err)
}
return nil
}
// initSQLStore initializes a new SQL based dataset storage system. It
// presumes that the database and been create and an appropriate
// database user has been created outside the dataset provided tooling.
// It uses a DNS in URI form where the "protocol" element identifies
// the type of SQL database, e.g. sqlite would use "sqlite:", MySQL
// would use "mysql:". The rest of the URI is formed from a Go style
// DSN (data source name). These are SQL system specific but usually
// include things like db name, user, password to access the database.
//
// A collection using SQL database for storage is split into one table
// per collection. There is a "_collection" table which holds collection
// wide properties, e.g. collection name, create, read, update, delete
// properties, versioning status, a copy of the codemeta.json document
// in a JSON column, the name of the collection in a column,
//
// InitSQLStore takes a *Collection and initializes the SQL database
// tables for a collection.
//
// NOTE: A SQL store still has a dataset named directory containing
// both the collection.json and codemeta.json file but it lacks a
// pairtree since that is where the object will be stored. Any
// attachments, if allowed, are stored in an S3 like bucket (e.g. via
// minio).
func (c *Collection) initSQLStore() error {
now := time.Now()
today := now.Format(datestamp)
c.DatasetVersion = Version
c.Created = now.UTC().Format(timestampUTC)
// Split see if c.Name path exists
if _, err := os.Stat(c.Name); os.IsNotExist(err) {
// Create directoriess if needed
if err := os.MkdirAll(c.Name, 0700); err != nil {
return fmt.Errorf("cannot create collect %q, %s", c.Name, err)
}
} else {
return fmt.Errorf("%q already exists", c.Name)
}
fullName := c.Name
basename := path.Base(c.Name)
colName := path.Join(fullName, "collection.json")
c.Name = basename
src, err := JSONMarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("cannot encode %q, %s", colName, err)
}
c.Name = fullName
if err := ioutil.WriteFile(colName, src, 0600); err != nil {
return fmt.Errorf("failed to create %q %s", colName, err)
}
// Create a default codemeta.json file in the directory
//today := time.Now().Format(datestamp)
m := map[string]string{
"{today}": today,
"{c_name}": path.Base(c.Name),
"{app_name}": path.Base(os.Args[0]),
"{version}": Version,
}
src = BytesProcessor(m, []byte(`{
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
"@type": "SoftwareSourceCode",
"dateCreated": "{today}",
"name": "{c_name}",
"version": "0.0.0",
"releaseNotes": "This is a {app_name} {version} collection",
"developmentStatus": "concept",
"softwareRequirements": [
"https://github.com/caltechlibrary/dataset"
]
}`))
cmName := path.Join(fullName, "codemeta.json")
if err := ioutil.WriteFile(cmName, src, 0664); err != nil {
return fmt.Errorf("failed to create %q, %s", cmName, err)
}
//NOTE: the collection's table needs to be created using the
// SQLStore's Init method..
c.SQLStore, err = SQLStoreInit(fullName, c.DsnURI)
return err
}
// Init - creates a new collection and opens it. It takes a name
// (e.g. directory holding the collection.json and codemeta.josn files)
// and an optional DSN in URI form. The default storage engine is a
// pairtree (i.e. PTSTORE) but some SQL storage engines are supported.
//
// If a DSN URI is a non-empty string then it is the SQL storage engine
// is used. The database and user access in the SQL engine needs be setup
// before you can successfully intialized your dataset collection.
// Currently three SQL database engines are support, SQLite3 or MySQL 8.
// You select the SQL storage engine by forming a URI consisting of a
// "protocol" (e.g. "sqlite", "mysql", "postgres"), the protocol
// delimiter "://" and a Go SQL supported DSN based on the database
// driver implementation.
//
// A MySQL 8 DSN URI would look something like
//
// `mysql://DB_USER:DB_PASSWD@PROTOCAL_EXPR/DB_NAME`
//
// The one for SQLite3
//
// `sqlite://FILENAME_FOR_SQLITE_DATABASE`
//
// NOTE: The DSN URI is stored in the collections.json. The file should
// NOT be world readable as that will expose your database password. You
// can remove the DSN URI after initializing your collection but will then
// need to provide the DATASET_DSN_URI envinronment variable so you can
// open your database successfully.
//
// For PTSTORE the access value can be left blank.
//
// ```
//
// var (
// c *Collection
// err error
// )
// name := "my_collection.ds"
// c, err = dataset.Init(name, "")
// if err != nil {
// // ... handle error
// }
// defer c.Close()
//
// ```
//
// For a sqlstore collection we need to pass the "access" value. This
// is the file containing a DNS or environment variables formating a DSN.
//
// ```
//
// var (
// c *Collection
// err error
// )
// name := "my_collection.ds"
// dsnURI := "sqlite://collection.db"
// c, err = dataset.Init(name, dsnURI)
// if err != nil {
// // ... handle error
// }
// defer c.Close()
//
// ```
func Init(name string, dsnURI string) (*Collection, error) {
var (
err error
)
c := new(Collection)
c.Name = name
c.DsnURI = dsnURI
if dsnURI == "" {
//c.StoreType = PTSTORE
c.StoreType = SQLSTORE
c.DsnURI = "sqlite://collection.db"
} else if dsnURI == "pairtree" {
c.StoreType = PTSTORE
} else {
c.StoreType = SQLSTORE
}
switch c.StoreType {
case PTSTORE:
err = c.initPTStore()
case SQLSTORE:
err = c.initSQLStore()
default:
return nil, fmt.Errorf("%q storage type not supported", c.StoreType)
}
if err != nil {
return nil, err
}
return Open(name)
}
// Codemeta returns a copy of the codemeta.json file content found
// in the collection directory. The collection must be previous open.
//
// ```
//
// name := "my_collection.ds"
// c, err := dataset.Open(name)
// if err != nil {
// ...
// }
// defer c.Close()
// src, err := c.Metadata()
// if err != nil {
// ...
// }
// ioutil.WriteFile("codemeta.json", src, 664)
//
// ```
func (c *Collection) Codemeta() ([]byte, error) {
fName := path.Join(c.Name, "codemeta.json")
src, err := ioutil.ReadFile(fName)
if err != nil {
return nil, fmt.Errorf("failed to read %q, %s", fName, err)
}
return src, nil
}
// UpdateMetadata imports new codemeta citation information replacing
// the previous version. Collection must be open.
//
// ```
//
// name := "my_collection.ds"
// codemetaFilename := "../codemeta.json"
// c, err := dataset.Open(name)
// if err != nil {
// ...
// }
// defer c.Close()
// c.UpdateMetadata(codemetaFilename)
//
// ```
func (c *Collection) UpdateMetadata(fName string) error {
src, err := ioutil.ReadFile(fName)
if err != nil {
return fmt.Errorf("failed to read %q, %s", fName, err)
}
if err := ioutil.WriteFile(path.Join(c.Name, "codemeta.json"), src, 0664); err != nil {
return fmt.Errorf("failed to write %q, %s", path.Join(c.Name, "codemeta.json"), err)
}
return nil
}
//
// The following are aliases to the storage system implementation.
//
// Create store a an object in the collection. Object will get
// converted to JSON source then stored. Collection must be open.
// A Go `map[string]interface{}` is a common way to handle ad-hoc
// JSON data in gow. Use `CreateObject()` to store structured
// data.
//
// ```
//
// key := "123"
// obj := map[]*interface{}{ "one": 1, "two": 2 }
// if err := c.Create(key, obj); err != nil {
// ...
// }
//
// ```
func (c *Collection) Create(key string, obj map[string]interface{}) error {
src, err := JSONMarshalIndent(obj, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal JSON for %s, %s", key, err)
}
switch c.StoreType {
case PTSTORE:
if c.PTStore != nil {
return c.PTStore.Create(key, src)
}
case SQLSTORE:
if c.SQLStore != nil {
return c.SQLStore.Create(key, src)
}
default:
return fmt.Errorf("%q not supported", c.StoreType)
}
return fmt.Errorf("%s not open", c.Name)
}
// CreateObject is used to store structed data in a dataset collection.
// The object needs to be defined as a Go struct notated approriately
// with the domain markup for working with json.
//
// ```
//
// import (
// "encoding/json"
// "fmt"
// "os"
// )
//
// type Record struct {
// ID string `json:"id"`
// Name string `json:"name,omitempty"`
// EMail string `json:"email,omitempty"`
// }
//
// func main() {
// c, err := dataset.Open("friends.ds")
// if err != nil {
// fmt.Fprintf(os.Stderr, "%s", err)
// os.Exit(1)
// }
// defer c.Close()
//
// obj := &Record{
// ID: "mojo",
// Name: "Mojo Sam",
// EMail: "[email protected]",
// }
// if err := c.CreateObject(obj.ID, obj); err != nil {
// fmt.Fprintf(os.Stderr, "%s", err)
// os.Exit(1)
// }
// fmt.Printf("OK\n")
// os.Exit(0)
// }
//
// ```
func (c *Collection) CreateObject(key string, obj interface{}) error {
src, err := JSONMarshalIndent(obj, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal JSON for %s, %s", key, err)
}
switch c.StoreType {
case PTSTORE:
if c.PTStore != nil {
return c.PTStore.Create(key, src)
}
case SQLSTORE:
if c.SQLStore != nil {
return c.SQLStore.Create(key, src)
}
default:
return fmt.Errorf("%q not supported", c.StoreType)
}
return fmt.Errorf("%s not open", c.Name)
}
// CreateJSON is used to store JSON directory into a dataset collection.
// NOTE: the JSON is NOT validated.
//
// ```
//
// import (
// "fmt"
// "os"
// )
//
// func main() {
// c, err := dataset.Open("friends.ds")
// if err != nil {
// fmt.Fprintf(os.Stderr, "%s", err)
// os.Exit(1)
// }
// defer c.Close()
//
// src := []byte(`{ "ID": "mojo", "Name": "Mojo Sam", "EMail": "[email protected]" }`)
// if err := c.CreateJSON("modo", src); err != nil {
// fmt.Fprintf(os.Stderr, "%s", err)
// os.Exit(1)
// }
// fmt.Printf("OK\n")
// os.Exit(0)
// }
//
// ```
func (c *Collection) CreateJSON(key string, src []byte) error {
switch c.StoreType {
case PTSTORE:
if c.PTStore != nil {
return c.PTStore.Create(key, src)
}
case SQLSTORE:
if c.SQLStore != nil {
return c.SQLStore.Create(key, src)
}
default:
return fmt.Errorf("%q not supported", c.StoreType)
}
return fmt.Errorf("%s not open", c.Name)
}
// Read retrieves a map[string]inteferface{} from the collection,
// unmarshals it and updates the object pointed to by the map.
//
// ```
//
// obj := map[string]interface{}{}
//
// key := "123"
// if err := c.Read(key, &obj); err != nil {
// ...
// }
//
// ```
func (c *Collection) Read(key string, obj map[string]interface{}) error {
var (
src []byte
err error
)
switch c.StoreType {
case PTSTORE:
src, err = c.PTStore.Read(key)
case SQLSTORE:
src, err = c.SQLStore.Read(key)
default:
return fmt.Errorf("%q not supported", c.StoreType)
}
if err != nil {
return fmt.Errorf("failed to read %s, %s", key, err)
}
return JSONUnmarshal(src, &obj)
}
// ReadObject retrieves structed data via Go's general inteferface{} type.
// The JSON document is retreived from the collection,
// unmarshaled and variable holding the struct is updated.
//
// ```
//
// type Record struct {
// ID string `json:"id"`
// Name string `json:"name,omitempty"`
// EMail string `json:"email,omitempty"`
// }
//
// // ...
//
// var obj *Record
//
// key := "123"
// if err := c.Read(key, &obj); err != nil {
// // ... handle error
// }
//
// ```
func (c *Collection) ReadObject(key string, obj interface{}) error {
var (
src []byte
err error
)
switch c.StoreType {
case PTSTORE:
src, err = c.PTStore.Read(key)
case SQLSTORE:
src, err = c.SQLStore.Read(key)
default:
return fmt.Errorf("%q not supported", c.StoreType)
}
if err != nil {
return fmt.Errorf("failed to read %s, %s", key, err)
}
decoder := json.NewDecoder(bytes.NewReader(src))
decoder.UseNumber()
if err := decoder.Decode(&obj); err != nil {
return err
}
return nil
}
// ReadJSON retrieves JSON stored in a dataset collection for
// a given key. NOTE: It does not validate the JSON
//
// ```
//
// key := "123"
// src, err := c.ReadJSON(key)
// if err != nil {
// // ... handle error
// }
//
// ```
func (c *Collection) ReadJSON(key string) ([]byte, error) {
var (
src []byte
err error
)
switch c.StoreType {
case PTSTORE:
src, err = c.PTStore.Read(key)
case SQLSTORE:
src, err = c.SQLStore.Read(key)
default:
return nil, fmt.Errorf("%q not supported", c.StoreType)
}
if err != nil {
return src, fmt.Errorf("failed to read %s, %s", key, err)
}
return src, nil
}
// ReadJSONVersion retrieves versioned JSON record stored in a
// dataset collection for a given key and semver.
// NOTE: It does not validate the JSON
//
// ```
//
// key := "123"
// semver := "0.0.2"
// src, err := c.ReadVersionJSON(key, semver)
// if err != nil {
// // ... handle error
// }
//
// ```
func (c *Collection) ReadJSONVersion(key string, semver string) ([]byte, error) {
var (
src []byte
err error
)
switch c.StoreType {
case PTSTORE:
src, err = c.PTStore.ReadVersion(key, semver)
case SQLSTORE:
src, err = c.SQLStore.ReadVersion(key, semver)
default:
return nil, fmt.Errorf("%q not supported", c.StoreType)
}
if err != nil {
return src, fmt.Errorf("failed to read %s, %s", key, err)
}
return src, nil
}
// Versions retrieves a list of versions available for a JSON document if
// versioning is enabled for the collection.
//
// ```
//
// key, version := "123", "0.0.1"
// if versions, err := Versions(key); err != nil {
// ...
// }
//
// ```
func (c *Collection) Versions(key string) ([]string, error) {
var (
versions []string
err error
)
switch c.StoreType {
case PTSTORE:
versions, err = c.PTStore.Versions(key)
case SQLSTORE:
versions, err = c.SQLStore.Versions(key)
default:
return nil, fmt.Errorf("%q not supported", c.StoreType)
}
if err != nil {
return nil, fmt.Errorf("failed to read %s, %s", key, err)
}
return versions, err
}
// ReadVersion retrieves a specific vesion from the collection for the given object.
//
// ```
//
// var obj map[string]interface{}
//
// key, version := "123", "0.0.1"
// if err := ReadVersion(key, version, &obj); err != nil {
// ...
// }
//
// ```
func (c *Collection) ReadVersion(key string, version string, obj map[string]interface{}) error {
var (
src []byte
err error
)
switch c.StoreType {
case PTSTORE:
src, err = c.PTStore.ReadVersion(key, version)
case SQLSTORE:
src, err = c.SQLStore.ReadVersion(key, version)
default:
return fmt.Errorf("%q not supported", c.StoreType)
}
if err != nil {
return fmt.Errorf("failed to read %s, %s", key, err)
}
return JSONUnmarshal(src, &obj)
}
// ReadObjectVersion retrieves a specific vesion from the collection
// for the given object.
//
// ```
//
// type Record srtuct {
// // ... structure def goes here.
// }
//
// var obj = *Record
//
// key, version := "123", "0.0.1"
// if err := ReadObjectVersion(key, version, &obj); err != nil {
// ...
// }
//
// ```
func (c *Collection) ReadObjectVersion(key string, version string, obj interface{}) error {
var (
src []byte
err error
)
switch c.StoreType {
case PTSTORE:
src, err = c.PTStore.ReadVersion(key, version)
case SQLSTORE:
src, err = c.SQLStore.ReadVersion(key, version)
default:
return fmt.Errorf("%q not supported", c.StoreType)
}
if err != nil {
return fmt.Errorf("failed to read %s, %s", key, err)
}
decoder := json.NewDecoder(bytes.NewReader(src))
decoder.UseNumber()
if err := decoder.Decode(&obj); err != nil {
return err
}
return nil
}
// Update replaces a JSON document in the collection with a new one.
// If the collection is versioned then it creates a new versioned copy
// and updates the "current" version to use it.
//
// ```
//
// key := "123"
// obj["three"] = 3
// if err := c.Update(key, obj); err != nil {
// ...
// }
//
// ```
func (c *Collection) Update(key string, obj map[string]interface{}) error {
src, err := JSONMarshalIndent(obj, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal JSON for %s, %s", key, err)
}
switch c.StoreType {
case PTSTORE:
if c.PTStore != nil {
return c.PTStore.Update(key, src)
}
case SQLSTORE:
if c.SQLStore != nil {
return c.SQLStore.Update(key, src)
}
default:
return fmt.Errorf("%q not supported", c.StoreType)
}
return fmt.Errorf("%s not open", c.Name)
}
// UpdateObject replaces a JSON document in the collection with a new one.
// If the collection is versioned then it creates a new versioned copy
// and updates the "current" version to use it.
//
// ```
//
// type Record struct {
// // ... structure def goes here.
// Three int `json:"three"`
// }
//
// var obj = *Record
//
// key := "123"
// obj := &Record {
// Three: 3,
// }
// if err := c.Update(key, obj); err != nil {
// // ... handle error
// }
//
// ```
func (c *Collection) UpdateObject(key string, obj interface{}) error {
src, err := JSONMarshalIndent(obj, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal JSON for %s, %s", key, err)
}
switch c.StoreType {
case PTSTORE:
if c.PTStore != nil {
return c.PTStore.Update(key, src)
}
case SQLSTORE:
if c.SQLStore != nil {
return c.SQLStore.Update(key, src)