Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(461)

Unified Diff: service/datastore/serialize.go

Issue 1285703002: Add testable interface for datastore. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: rebase Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: service/datastore/serialize.go
diff --git a/service/datastore/serialize.go b/service/datastore/serialize.go
index 30480231f7ad6de2060767b3f4a96c590f7321a2..4848cd982141059162f89be4b9eb0e8e56c858db 100644
--- a/service/datastore/serialize.go
+++ b/service/datastore/serialize.go
@@ -15,6 +15,8 @@ import (
"github.com/luci/luci-go/common/cmpbin"
)
+const MaxIndexColumns = 64
dnj (Google) 2015/08/14 22:53:54 Can you document why 64?
iannucci 2015/08/15 01:59:25 done
+
// WritePropertyMapDeterministic allows tests to make WritePropertyMap
// deterministic.
var WritePropertyMapDeterministic = false
@@ -365,6 +367,85 @@ func (pm PropertyMap) Read(buf Buffer, context KeyContext, appid, namespace stri
return
}
+func (c *IndexColumn) Write(buf Buffer) (err error) {
+ defer recoverTo(&err)
+
+ if c.Direction == ASCENDING {
+ panicIf(buf.WriteByte(0))
+ } else {
+ panicIf(buf.WriteByte(1))
+ }
+ _, err = cmpbin.WriteString(buf, c.Property)
+ return
+}
+
+func (c *IndexColumn) Read(buf Buffer) (err error) {
+ defer recoverTo(&err)
+
+ dir, err := buf.ReadByte()
+ panicIf(err)
+
+ c.Direction = dir != 0
dnj (Google) 2015/08/14 22:53:54 Since you use constants (ASCENDING) for writing, y
iannucci 2015/08/15 01:59:25 Done.
+ c.Property, _, err = cmpbin.ReadString(buf)
+ return err
+}
+
+func (i *IndexDefinition) Write(buf Buffer) (err error) {
+ defer recoverTo(&err)
+
+ if i.Builtin() {
+ panicIf(buf.WriteByte(0))
+ } else {
+ panicIf(buf.WriteByte(1))
+ }
+ _, err = cmpbin.WriteString(buf, i.Kind)
+ panicIf(err)
+ if !i.Ancestor {
+ panicIf(buf.WriteByte(0))
+ } else {
+ panicIf(buf.WriteByte(1))
+ }
+ _, err = cmpbin.WriteUint(buf, uint64(len(i.SortBy)))
+ panicIf(err)
+ for _, sb := range i.SortBy {
+ panicIf(sb.Write(buf))
+ }
+ return
+}
+
+func (i *IndexDefinition) Read(buf Buffer) (err error) {
+ defer recoverTo(&err)
+
+ // discard builtin/complex byte
+ _, err = buf.ReadByte()
+ panicIf(err)
+
+ i.Kind, _, err = cmpbin.ReadString(buf)
+ panicIf(err)
+
+ anc, err := buf.ReadByte()
+ panicIf(err)
+
+ i.Ancestor = anc == 1
+
+ numSorts, _, err := cmpbin.ReadUint(buf)
+ panicIf(err)
+
+ if numSorts > MaxIndexColumns {
+ return fmt.Errorf("datastore: Got over %d sort orders: %d",
+ MaxIndexColumns, numSorts)
+ }
+
+ if numSorts > 0 {
+ i.SortBy = make([]IndexColumn, numSorts)
+ for idx := range i.SortBy {
+ panicIf(i.SortBy[idx].Read(buf))
+ }
+ }
+
+ return
+}
+
type parseError error
func panicIf(err error) {

Powered by Google App Engine
This is Rietveld 408576698