This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
db.go
1178 lines (961 loc) · 23.4 KB
/
db.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 syncstorage
import (
"database/sql"
"fmt"
"strconv"
"strings"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
_ "github.com/mattn/go-sqlite3"
. "github.com/mostlygeek/go-debug"
)
var dbDebug = Debug("syncstorage:db")
var (
ErrNotFound = errors.New("Not Found")
ErrNotImplemented = errors.New("Not Implemented")
ErrNothingToDo = errors.New("Nothing to do")
ErrInvalidBSOId = errors.New("Invalid BSO Id")
ErrInvalidCollectionId = errors.New("Invalid Collection Id")
ErrInvalidCollectionName = errors.New("Invalid Collection Name")
ErrInvalidPayload = errors.New("Invalid Payload")
ErrInvalidSortIndex = errors.New("Invalid Sort Index")
ErrInvalidTTL = errors.New("Invalid TTL")
ErrInvalidLimit = errors.New("Invalid LIMIT")
ErrInvalidOffset = errors.New("Invalid OFFSET")
ErrInvalidNewer = errors.New("Invalid NEWER than")
)
// dbTx allows passing of sql.DB or sql.Tx
type dbTx interface {
Exec(string, ...interface{}) (sql.Result, error)
Query(string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
}
type SortType int
const (
SORT_NONE SortType = iota
SORT_NEWEST
SORT_OLDEST
SORT_INDEX
// The default TTL is to never expire. Use 100 years
// which should be enough (in milliseconds)
DEFAULT_BSO_TTL = 100 * 365 * 24 * 60 * 60 * 1000
STORAGE_LAST_MODIFIED = "Storage Last Modified"
)
type CollectionInfo struct {
Name string
BSOs int
Storage int
Modified int
}
// PostBSOs takes a set of BSO and performs an Insert or Update on
// each of them.
type PostResults struct {
Modified int
Success []string
Failed map[string][]string
}
func NewPostResults(modified int) *PostResults {
return &PostResults{
Modified: modified,
Success: make([]string, 0),
Failed: make(map[string][]string),
}
}
func (p *PostResults) AddSuccess(bId ...string) {
p.Success = append(p.Success, bId...)
}
func (p *PostResults) AddFailure(bId string, reasons ...string) {
p.Failed[bId] = reasons
}
// GetResults holds search results for BSOs, this is what getBSOs() returns
type GetResults struct {
BSOs []*BSO
More bool
Offset int
}
func (g *GetResults) String() string {
s := fmt.Sprintf("Total: %d, More: %v, Offset: %d\nBSOs:\n",
len(g.BSOs), g.More, g.Offset)
for _, b := range g.BSOs {
s += fmt.Sprintf(" Id:%s, Modified:%d, SortIndex:%d, TTL:%d, %s\n",
b.Id, b.Modified, b.SortIndex, b.TTL, b.Payload)
}
return s
}
type DBPageStats struct {
Size int
Total int
Free int
}
// FreePercent calculates how much of total space is used up by
// free pages (empty of data)
func (s *DBPageStats) FreePercent() int {
if s.Total == 0 {
return 0
}
return int(float32(s.Free) / float32(s.Total) * 100)
}
type DB struct {
sync.RWMutex
// sqlite database path
Path string
db *sql.DB
}
type Config struct {
CacheSize int
}
func (d *DB) OpenWithConfig(conf *Config) (err error) {
d.db, err = sql.Open("sqlite3", d.Path)
if err != nil {
return
}
// settings to apply to the database
pragmas := []string{
"PRAGMA page_size=4096;",
"PRAGMA journal_mode=WAL;",
}
if conf != nil {
if log.GetLevel() == log.DebugLevel {
log.WithFields(log.Fields{
"cache_size": conf.CacheSize,
}).Debug("db config")
}
pragmas = append(pragmas, fmt.Sprintf("PRAGMA cache_size=%d;", conf.CacheSize))
}
for _, p := range pragmas {
if _, err = d.db.Exec(p); err != nil {
return errors.Wrapf(err, "Could not set PRAGMA: %s", p)
}
}
var schemaVersion int
if err := d.db.QueryRow("PRAGMA schema_version;").Scan(&schemaVersion); err != nil {
return err
}
// Initialize a new database with all the current schemas concatenated together
if schemaVersion == 0 {
tx, err := d.db.Begin()
if err != nil {
return err
}
if _, err := tx.Exec(SCHEMA_0 + SCHEMA_1); err != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
return rollbackErr
} else {
return err
}
} else {
if err := tx.Commit(); err != nil {
return err
}
}
} else {
// Migrate schema to the latest version. At the time of this
// comment there is only SCHEMA_1. Considering the rate of
// schema change, we can probably just keep it simple yet
// slightly more verbose using, `if userVersion == ...` statements
var userVersion int
if err := d.db.QueryRow("PRAGMA user_version;").Scan(&userVersion); err != nil {
return err
}
// SCHEMA_0 did not alter PRAGMA user_version so it defaults to 0
if userVersion == 0 {
tx, err := d.db.Begin()
if err != nil {
return err
}
if _, err := tx.Exec(SCHEMA_1); err != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
return rollbackErr
} else {
return err
}
} else {
if err := tx.Commit(); err != nil {
return err
}
}
}
// SCHEMA_1 sets PRAGMA user_version to 2 so the count
// of schemas applied is caught up and correct.
// putting this here for posterity and next schema upgrade
// if userVersion == 2 { ... }
}
return nil
}
func (d *DB) Open() (err error) {
return d.OpenWithConfig(nil)
}
func (d *DB) Close() {
if d.db != nil {
dbDebug("Closing: %s", d.Path)
d.db.Close()
}
}
func NewDB(path string, conf *Config) (*DB, error) {
d := &DB{Path: path}
if err := d.OpenWithConfig(conf); err != nil {
return nil, err
}
return d, nil
}
/*
The public functions in *DB control locking of the main object
so we can only have one read or write at a time. The actual database
work is handled by private functions.
*/
// LastModified returns the top level last modified timestamp
func (d *DB) LastModified() (int, error) {
d.Lock()
defer d.Unlock()
lastMod, err := getKey(d.db, STORAGE_LAST_MODIFIED)
if lastMod == "" || err != nil {
return 0, err
}
lastModInt64, err := strconv.ParseInt(lastMod, 10, 64)
if err != nil {
return 0, errors.Wrap(err, "Failed converting storage timestamp to int. UhOh?!")
}
return int(lastModInt64), nil
}
func (d *DB) GetCollectionId(name string) (id int, err error) {
d.Lock()
defer d.Unlock()
// return common collection id without touching the DB
// ew? yes, but it'll compile nice and fast
switch name {
case "clients":
return 1, nil
case "crypto":
return 2, nil
case "forms":
return 3, nil
case "history":
return 4, nil
case "keys":
return 5, nil
case "meta":
return 6, nil
case "bookmarks":
return 7, nil
case "prefs":
return 8, nil
case "tabs":
return 9, nil
case "passwords":
return 10, nil
case "addons":
return 11, nil
case "addresses":
return 12, nil
case "creditcards":
return 13, nil
}
if !CollectionNameOk(name) {
err = ErrInvalidCollectionName
return
}
err = d.db.QueryRow("SELECT Id FROM Collections where Name=?", name).Scan(&id)
if err == sql.ErrNoRows {
err = ErrNotFound
}
return
}
func (d *DB) GetCollectionModified(cId int) (modified int, err error) {
d.Lock()
defer d.Unlock()
err = d.db.QueryRow("SELECT modified FROM Collections where Id=?", cId).Scan(&modified)
if err == sql.ErrNoRows {
return 0, nil
}
return
}
func (d *DB) CreateCollection(name string) (cId int, err error) {
d.Lock()
defer d.Unlock()
if !CollectionNameOk(name) {
err = ErrInvalidCollectionName
return
}
tx, err := d.db.Begin()
if err != nil {
return 0, err
}
modified := Now()
dml := "INSERT INTO Collections (Name, Modified) VALUES (?,?)"
results, err := tx.Exec(dml, name, modified)
if err != nil {
tx.Rollback()
return 0, err
}
cId64, err := results.LastInsertId()
if err != nil {
tx.Rollback()
return 0, err
}
tx.Commit()
return int(cId64), nil
}
func (d *DB) DeleteCollection(cId int) (int, error) {
d.Lock()
defer d.Unlock()
tx, err := d.db.Begin()
if err != nil {
return 0, errors.Wrap(err, "Failed creating transaction")
}
dmlB := "DELETE FROM BSO WHERE CollectionId=?"
if _, err := tx.Exec(dmlB, cId); err != nil {
tx.Rollback()
return 0, errors.Wrapf(err, "Failed deleting collection: %d", cId)
}
if err := d.touchCollection(tx, cId, 0); err != nil {
tx.Rollback()
return 0, errors.Wrapf(err, "Failed resetting last modified for collection: %d", cId)
}
modified := Now()
if err := d.touchStorage(tx, modified); err != nil {
tx.Rollback()
return 0, errors.Wrapf(err, "Failed setting storage timestamp")
}
tx.Commit()
return modified, nil
}
// DeleteEverything will delete all BSOs, record when everything was deleted
// and vacuum to free up disk pages.
func (d *DB) DeleteEverything() (err error) {
d.Lock()
defer d.Unlock()
// delete all BSO data and keep the other metadata around
dml := `
DELETE FROM BSO;
INSERT OR REPLACE INTO KeyValues (Key, Value) VALUES ("DELETE_EVERYTHING_DATE", ?);
VACUUM;
`
_, err = d.db.Exec(dml, time.Now().Format(time.RFC3339))
return
}
func (d *DB) TouchCollection(cId, modified int) (err error) {
d.Lock()
defer d.Unlock()
return d.touchCollectionAndStorage(d.db, cId, modified)
}
// InfoCollections create a map of collection names to last modified times
func (d *DB) InfoCollections() (map[string]int, error) {
d.Lock()
defer d.Unlock()
rows, err := d.db.Query("SELECT Name,Modified FROM Collections WHERE Modified != 0")
if err != nil {
return nil, err
}
defer rows.Close()
results := make(map[string]int)
for rows.Next() {
var name string
var modified int
if err := rows.Scan(&name, &modified); err != nil {
return nil, err
}
results[name] = modified
}
return results, nil
}
func (d *DB) InfoQuota() (used, quota int, err error) {
d.Lock()
defer d.Unlock()
var u sql.NullInt64
query := `SELECT sum(PayloadSize) used
FROM BSO`
err = d.db.QueryRow(query).Scan(&u)
if err != nil {
if err == sql.ErrNoRows {
return 0, 0, nil
}
return
}
if u.Valid {
used = int(u.Int64)
return
} else {
return 0, 0, nil
}
}
func (d *DB) InfoCollectionUsage() (map[string]int, error) {
d.Lock()
defer d.Unlock()
query := `SELECT c.Name,sum(b.PayloadSize) used
FROM BSO b, Collections C
WHERE b.CollectionId=c.Id GROUP BY b.CollectionId`
rows, err := d.db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
results := make(map[string]int)
for rows.Next() {
var name string
var used int
if err := rows.Scan(&name, &used); err != nil {
return nil, err
}
results[name] = used
}
return results, nil
}
func (d *DB) InfoCollectionCounts() (map[string]int, error) {
d.Lock()
defer d.Unlock()
query := `SELECT c.Name, count(b.Id) count
FROM BSO b, Collections C
WHERE b.CollectionId=c.Id GROUP BY b.CollectionId`
rows, err := d.db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
results := make(map[string]int)
for rows.Next() {
var name string
var count int
if err := rows.Scan(&name, &count); err != nil {
return nil, err
}
results[name] = count
}
return results, nil
}
type PostBSOInput []*PutBSOInput
type PutBSOInput struct {
Id string `json:"id"`
Payload *string `json:"payload,omitempty"`
TTL *int `json:"ttl,omitempty"`
SortIndex *int `json:"sortindex,omitempty"`
}
func NewPutBSOInput(id string, payload *string, sortIndex, ttl *int) *PutBSOInput {
if ttl == nil {
t := DEFAULT_BSO_TTL
ttl = &t
}
return &PutBSOInput{Id: id, TTL: ttl, SortIndex: sortIndex, Payload: payload}
}
func (d *DB) PostBSOs(cId int, input PostBSOInput) (*PostResults, error) {
d.Lock()
defer d.Unlock()
tx, err := d.db.Begin()
if err != nil {
return nil, err
}
modified := Now() // same modified timestamp for all INSERT/UPDATES
results := NewPostResults(modified)
for _, data := range input {
err := d.putBSO(tx, cId, data.Id, modified, data.Payload, data.SortIndex, data.TTL)
if err != nil {
results.AddFailure(data.Id, err.Error())
continue
} else {
results.AddSuccess(data.Id)
}
}
// update the collection
err = d.touchCollectionAndStorage(tx, cId, modified)
if err != nil {
tx.Rollback()
return nil, err
}
tx.Commit()
return results, nil
}
// PutBSO creates or updates a BSO
func (d *DB) PutBSO(cId int, bId string, payload *string, sortIndex *int, ttl *int) (modified int, err error) {
d.Lock()
defer d.Unlock()
tx, err := d.db.Begin()
if err != nil {
return
}
modified = Now()
err = d.putBSO(tx, cId, bId, modified, payload, sortIndex, ttl)
if err != nil {
tx.Rollback()
return
}
// update the collection
err = d.touchCollectionAndStorage(tx, cId, modified)
if err != nil {
tx.Rollback()
return
}
tx.Commit()
return
}
func (d *DB) GetBSO(cId int, bId string) (b *BSO, err error) {
d.Lock()
defer d.Unlock()
b, err = d.getBSO(d.db, cId, bId)
return
}
func (d *DB) GetBSOs(
cId int,
ids []string,
older int,
newer int,
sort SortType,
limit int,
offset int) (r *GetResults, err error) {
d.Lock()
defer d.Unlock()
r, err = d.getBSOs(d.db, cId, ids, older, newer, sort, limit, offset)
return
}
func (d *DB) GetBSOModified(cId int, bId string) (modified int, err error) {
d.Lock()
defer d.Unlock()
err = d.db.QueryRow(`SELECT modified
FROM BSO
WHERE CollectionId=? and Id=? and TTL > ?`, cId, bId, Now()).Scan(&modified)
if err != nil {
if err == sql.ErrNoRows {
return 0, ErrNotFound
}
return 0, err
}
return
}
// DeleteBSO deletes a single BSO and returns the
// modified timestamp for the collection
func (d *DB) DeleteBSO(cId int, bId string) (int, error) {
return d.DeleteBSOs(cId, bId)
}
// DeleteBSOs deletes multiple BSO. It returns the modified
// timestamp for the collection on success
func (d *DB) DeleteBSOs(cId int, bIds ...string) (modified int, err error) {
d.Lock()
defer d.Unlock()
if log.GetLevel() == log.DebugLevel {
log.WithFields(log.Fields{
"cid": cId,
"bIds": bIds,
}).Debug("db DeleteBSOs")
}
tx, err := d.db.Begin()
if err != nil {
return
}
dml := "DELETE FROM BSO WHERE CollectionId=? AND Id IN (?" +
strings.Repeat(",?", len(bIds)-1) + ")"
// https://golang.org/doc/faq#convert_slice_of_interface
ids := make([]interface{}, len(bIds)+1)
ids[0] = cId
for i, v := range bIds {
ids[i+1] = v
}
_, err = tx.Exec(dml, ids...)
if err != nil {
tx.Rollback()
return
}
modified = Now()
// update the collection
err = d.touchCollectionAndStorage(tx, cId, modified)
if err != nil {
tx.Rollback()
return
}
tx.Commit()
return
}
// PurgeExpired removes all BSOs that have expired out
func (d *DB) PurgeExpired() (removed int, err error) {
d.Lock()
defer d.Unlock()
dmlBSO := "DELETE FROM BSO WHERE TTL <= ?"
r, err := d.db.Exec(dmlBSO, Now())
if err != nil {
return 0, err
}
purged, err := r.RowsAffected()
return int(purged), err
}
func (d *DB) Usage() (stats *DBPageStats, err error) {
d.Lock()
defer d.Unlock()
stats = &DBPageStats{}
err = d.db.QueryRow("PRAGMA page_count").Scan(&stats.Total)
if err != nil {
return nil, err
}
err = d.db.QueryRow("PRAGMA freelist_count").Scan(&stats.Free)
if err != nil {
return nil, err
}
err = d.db.QueryRow("PRAGMA page_size").Scan(&stats.Size)
if err != nil {
return nil, err
}
return
}
// Optimize recovers disk space by removing empty db pages
// if the number of free pages makes up more than `threshold`
// percent of the total pages
func (d *DB) Optimize(thresholdPercent int) (ItHappened bool, err error) {
stats, err := d.Usage()
if err != nil {
return
}
if stats.FreePercent() >= thresholdPercent {
err = d.Vacuum()
ItHappened = true
}
return
}
// SetKey inserts or replaces a key with the value
func (d *DB) SetKey(key, value string) error {
d.Lock()
defer d.Unlock()
return setKey(d.db, key, value)
}
// GetKey returns a previous key in the database
func (d *DB) GetKey(key string) (string, error) {
d.Lock()
defer d.Unlock()
return getKey(d.db, key)
}
func setKey(tx dbTx, key, value string) (err error) {
_, err = tx.Exec(`INSERT OR REPLACE INTO KeyValues (Key, Value) VALUES (?, ?)`, key, value)
return
}
func getKey(tx dbTx, key string) (value string, err error) {
query := "SELECT Value FROM KeyValues WHERE Key=?"
err = tx.QueryRow(query, key).Scan(&value)
if err == sql.ErrNoRows {
return "", nil
}
return
}
// Vacuum recovers free disk pages and reduces fragmentation of the
// data on disk. This could take a long time depending on the size
// of the database
func (d *DB) Vacuum() (err error) {
d.Lock()
defer d.Unlock()
_, err = d.db.Exec("VACUUM")
return
}
// touchCollection updates a collection's last-modified timestamp
func (d *DB) touchCollection(tx dbTx, cId, modified int) (err error) {
_, err = tx.Exec("UPDATE Collections SET modified=? WHERE Id=?", modified, cId)
return err
}
// touchStorage updates the storage level last modified timestamp.
// Added in issue #169 where the server didn't properly update
// timestamps on collection delete
func (d *DB) touchStorage(tx dbTx, modified int) error {
return setKey(tx, STORAGE_LAST_MODIFIED, strconv.Itoa(modified))
}
// touchCollectionAndStorage updates both the storage and a
// collection's last modified timestamp
func (d *DB) touchCollectionAndStorage(tx dbTx, cId, modified int) error {
if err := d.touchCollection(tx, cId, modified); err != nil {
return errors.Wrapf(err, "Failed updating timestamp on cId=%d", cId)
}
if err := d.touchStorage(tx, modified); err != nil {
return errors.Wrap(err, "Failed updating storage timestamp")
}
return nil
}
// putBSO will INSERT or UPDATE a BSO
func (d *DB) putBSO(tx dbTx,
cId int,
bId string,
modified int,
payload *string,
sortIndex *int,
ttl *int,
) (err error) {
if payload == nil && sortIndex == nil && ttl == nil {
err = ErrNothingToDo
return
}
if !BSOIdOk(bId) {
err = ErrInvalidBSOId
return
}
if sortIndex != nil && !SortIndexOk(*sortIndex) {
err = ErrInvalidSortIndex
return
}
if ttl != nil && !TTLOk(*ttl) {
err = ErrInvalidTTL
return
}
exists, err := d.bsoExists(tx, cId, bId)
if err != nil {
return
}
// Do an UPDATE or an INSERT
if exists == true {
var t *int
if ttl != nil {
tmp := *ttl
t = &tmp
}
return d.updateBSO(tx, cId, bId, modified, payload, sortIndex, t)
} else {
var p string
var s, t int
if payload == nil {
p = ""
} else {
p = *payload
}
if sortIndex == nil {
s = 0
} else {
s = *sortIndex
}
if ttl == nil {
t = DEFAULT_BSO_TTL
} else {
t = *ttl
}
return d.insertBSO(tx, cId, bId, modified, p, s, t)
}
}
// bsoExists checks if a BSO is in the database
func (d *DB) bsoExists(tx dbTx, cId int, bId string) (bool, error) {
var found int
query := "SELECT 1 FROM BSO WHERE CollectionId=? AND Id=?"
err := tx.QueryRow(query, cId, bId).Scan(&found)
if err == sql.ErrNoRows {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// getBSOs searches for bsos based on the api 1.5 criteria
func (d *DB) getBSOs(
tx dbTx,
cId int,
ids []string,
older int,
newer int,
sort SortType,
limit int,
offset int) (*GetResults, error) {
if !OffsetOk(offset) {
return nil, ErrInvalidOffset
}
if !LimitOk(limit) {
return nil, ErrInvalidLimit
}
if !NewerOk(newer) {
return nil, ErrInvalidNewer
}
cutOffTTL := Now()
query := "SELECT Id, SortIndex, Payload, Modified, TTL FROM BSO "
where := "WHERE CollectionId=? AND Modified < ? AND Modified > ? AND TTL > ?"
values := []interface{}{cId, older, newer, cutOffTTL}
if len(ids) > 0 {
// spec says only 100 ids at a time
if len(ids) > 100 {
ids = ids[0:100]
}
where += " AND Id IN (?" + strings.Repeat(",?", len(ids)-1) + ")"
for _, id := range ids {
values = append(values, id)
}
}
orderBy := ""
if sort == SORT_INDEX {
orderBy = "ORDER BY SortIndex DESC "
} else if sort == SORT_NEWEST {
orderBy = "ORDER BY Modified DESC "
} else if sort == SORT_OLDEST {
orderBy = "ORDER BY Modified ASC "
}
limitStmt := "LIMIT ?"
// fetch an extra row to detect if there are more
// rows that match the query conditions
if limit >= 0 {
values = append(values, limit+1)
} else {
values = append(values, limit)
}
if offset != 0 {
limitStmt += " OFFSET ?"
values = append(values, offset)
}
resultQuery := fmt.Sprintf("%s %s %s %s", query, where, orderBy, limitStmt)
rows, err := tx.Query(resultQuery, values...)
defer rows.Close()
if log.GetLevel() == log.DebugLevel {
log.WithFields(log.Fields{
"query": resultQuery,
"values": values,
}).Debug("db getBSOs")
}
if err != nil {
return nil, err
}
bsos := make([]*BSO, 0)
for rows.Next() {
b := &BSO{}
if err := rows.Scan(&b.Id, &b.SortIndex, &b.Payload, &b.Modified, &b.TTL); err != nil {
return nil, err
} else {
bsos = append(bsos, b)
}
}
var more bool