OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "reflect" | 10 "reflect" |
11 "sort" | 11 "sort" |
12 "time" | 12 "time" |
13 | 13 |
14 "appengine" | 14 "appengine" |
15 "appengine/datastore" | 15 "appengine/datastore" |
16 | 16 |
17 "github.com/luci/gkvlite" | 17 "github.com/luci/gkvlite" |
18 » "github.com/luci/luci-go/common/funnybase" | 18 » "github.com/luci/luci-go/common/cmpbin" |
19 ) | 19 ) |
20 | 20 |
21 type typData struct { | 21 type typData struct { |
22 noIndex bool | 22 noIndex bool |
23 typ propValType | 23 typ propValType |
24 data interface{} | 24 data interface{} |
25 } | 25 } |
26 | 26 |
27 func newTypData(noIndex bool, v interface{}) (ret *typData, err error) { | 27 func newTypData(noIndex bool, v interface{}) (ret *typData, err error) { |
28 typ := pvUNKNOWN | 28 typ := pvUNKNOWN |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 func (td *typData) WriteBinary(buf *bytes.Buffer, nso nsOption) error { | 63 func (td *typData) WriteBinary(buf *bytes.Buffer, nso nsOption) error { |
64 typb := byte(td.typ) | 64 typb := byte(td.typ) |
65 if td.noIndex { | 65 if td.noIndex { |
66 typb |= 0x80 | 66 typb |= 0x80 |
67 } | 67 } |
68 buf.WriteByte(typb) | 68 buf.WriteByte(typb) |
69 switch td.typ { | 69 switch td.typ { |
70 case pvNull, pvBoolFalse, pvBoolTrue: | 70 case pvNull, pvBoolFalse, pvBoolTrue: |
71 return nil | 71 return nil |
72 case pvInt: | 72 case pvInt: |
73 » » funnybase.Write(buf, td.data.(int64)) | 73 » » cmpbin.WriteInt(buf, td.data.(int64)) |
74 case pvFloat: | 74 case pvFloat: |
75 writeFloat64(buf, td.data.(float64)) | 75 writeFloat64(buf, td.data.(float64)) |
76 case pvStr: | 76 case pvStr: |
77 writeString(buf, td.data.(string)) | 77 writeString(buf, td.data.(string)) |
78 case pvBytes: | 78 case pvBytes: |
79 if td.noIndex { | 79 if td.noIndex { |
80 writeBytes(buf, td.data.([]byte)) | 80 writeBytes(buf, td.data.([]byte)) |
81 } else { | 81 } else { |
82 writeBytes(buf, td.data.(datastore.ByteString)) | 82 writeBytes(buf, td.data.(datastore.ByteString)) |
83 } | 83 } |
(...skipping 19 matching lines...) Expand all Loading... |
103 td.noIndex = (typb & 0x80) != 0 // highbit means noindex | 103 td.noIndex = (typb & 0x80) != 0 // highbit means noindex |
104 td.typ = propValType(typb & 0x7f) | 104 td.typ = propValType(typb & 0x7f) |
105 switch td.typ { | 105 switch td.typ { |
106 case pvNull: | 106 case pvNull: |
107 td.data = nil | 107 td.data = nil |
108 case pvBoolTrue: | 108 case pvBoolTrue: |
109 td.data = true | 109 td.data = true |
110 case pvBoolFalse: | 110 case pvBoolFalse: |
111 td.data = false | 111 td.data = false |
112 case pvInt: | 112 case pvInt: |
113 » » td.data, err = funnybase.Read(buf) | 113 » » td.data, _, err = cmpbin.ReadInt(buf) |
114 case pvFloat: | 114 case pvFloat: |
115 td.data, err = readFloat64(buf) | 115 td.data, err = readFloat64(buf) |
116 case pvStr: | 116 case pvStr: |
117 td.data, err = readString(buf) | 117 td.data, err = readString(buf) |
118 case pvBytes: | 118 case pvBytes: |
119 b := []byte(nil) | 119 b := []byte(nil) |
120 if b, err = readBytes(buf); err != nil { | 120 if b, err = readBytes(buf); err != nil { |
121 return err | 121 return err |
122 } | 122 } |
123 if td.noIndex { | 123 if td.noIndex { |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 | 504 |
505 func (pl *propertyList) MarshalBinary() ([]byte, error) { | 505 func (pl *propertyList) MarshalBinary() ([]byte, error) { |
506 cols, err := pl.collate() | 506 cols, err := pl.collate() |
507 if err != nil || len(cols) == 0 { | 507 if err != nil || len(cols) == 0 { |
508 return nil, err | 508 return nil, err |
509 } | 509 } |
510 | 510 |
511 pieces := make([][]byte, 0, len(*pl)*2+1) | 511 pieces := make([][]byte, 0, len(*pl)*2+1) |
512 for _, pv := range cols { | 512 for _, pv := range cols { |
513 // TODO(riannucci): estimate buffer size better. | 513 // TODO(riannucci): estimate buffer size better. |
514 » » buf := bytes.NewBuffer(make([]byte, 0, funnybase.MaxFunnyBaseLen
64+len(pv.name))) | 514 » » buf := bytes.NewBuffer(make([]byte, 0, cmpbin.MaxIntLen64+len(pv
.name))) |
515 writeString(buf, pv.name) | 515 writeString(buf, pv.name) |
516 err := pv.WriteBinary(buf) | 516 err := pv.WriteBinary(buf) |
517 if err != nil { | 517 if err != nil { |
518 return nil, err | 518 return nil, err |
519 } | 519 } |
520 pieces = append(pieces, buf.Bytes()) | 520 pieces = append(pieces, buf.Bytes()) |
521 } | 521 } |
522 return bytes.Join(pieces, nil), nil | 522 return bytes.Join(pieces, nil), nil |
523 } | 523 } |
524 | 524 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 // These two are problematic, because they force us to bind to the appen
gine | 602 // These two are problematic, because they force us to bind to the appen
gine |
603 // SDK code. If we can drop support for these and turn them into hard er
rors, | 603 // SDK code. If we can drop support for these and turn them into hard er
rors, |
604 // that could let us decouple from the various appengine SDKs. Maybe. | 604 // that could let us decouple from the various appengine SDKs. Maybe. |
605 pvKey // TODO(riannucci): remove support for this (use a string) | 605 pvKey // TODO(riannucci): remove support for this (use a string) |
606 pvBlobKey // TODO(riannucci): remove support for this (use a string) | 606 pvBlobKey // TODO(riannucci): remove support for this (use a string) |
607 | 607 |
608 pvUNKNOWN | 608 pvUNKNOWN |
609 ) | 609 ) |
610 | 610 |
611 func (p *pvals) ReadBinary(buf *bytes.Buffer) error { | 611 func (p *pvals) ReadBinary(buf *bytes.Buffer) error { |
612 » n, err := funnybase.ReadUint(buf) | 612 » n, _, err := cmpbin.ReadUint(buf) |
613 if err != nil { | 613 if err != nil { |
614 return err | 614 return err |
615 } | 615 } |
616 | 616 |
617 p.vals = make([]*typData, n) | 617 p.vals = make([]*typData, n) |
618 for i := range p.vals { | 618 for i := range p.vals { |
619 p.vals[i] = &typData{} | 619 p.vals[i] = &typData{} |
620 err := p.vals[i].ReadBinary(buf, withNS, "") | 620 err := p.vals[i].ReadBinary(buf, withNS, "") |
621 if err != nil { | 621 if err != nil { |
622 return err | 622 return err |
623 } | 623 } |
624 } | 624 } |
625 | 625 |
626 return nil | 626 return nil |
627 } | 627 } |
628 | 628 |
629 func (p *pvals) WriteBinary(buf *bytes.Buffer) error { | 629 func (p *pvals) WriteBinary(buf *bytes.Buffer) error { |
630 » funnybase.WriteUint(buf, uint64(len(p.vals))) | 630 » cmpbin.WriteUint(buf, uint64(len(p.vals))) |
631 for _, v := range p.vals { | 631 for _, v := range p.vals { |
632 if err := v.WriteBinary(buf, withNS); err != nil { | 632 if err := v.WriteBinary(buf, withNS); err != nil { |
633 return err | 633 return err |
634 } | 634 } |
635 } | 635 } |
636 return nil | 636 return nil |
637 } | 637 } |
OLD | NEW |