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

Unified Diff: service/datastore/properties.go

Issue 1363063002: Add ability to estimate the size of a PropertyMap (Closed) Base URL: https://github.com/luci/gae.git@minor_tweak
Patch Set: fix errcheck madness Created 5 years, 3 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
« no previous file with comments | « service/datastore/key.go ('k') | service/datastore/size_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: service/datastore/properties.go
diff --git a/service/datastore/properties.go b/service/datastore/properties.go
index 9890a64c6aa3ce2ae7168f2f03e8b0447e10835b..60d8b423ad6dfc4fe7399a0570df4e7fbba883d8 100644
--- a/service/datastore/properties.go
+++ b/service/datastore/properties.go
@@ -588,6 +588,33 @@ func (s PropertySlice) Len() int { return len(s) }
func (s PropertySlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s PropertySlice) Less(i, j int) bool { return s[i].Less(&s[j]) }
+// EstimateSize estimates the amount of space that this Property would consume
+// if it were committed as part of an entity in the real production datastore.
+//
+// It uses https://cloud.google.com/appengine/articles/storage_breakdown?csw=1
+// as a guide for these values.
+func (p *Property) EstimateSize() int64 {
+ switch p.Type() {
+ case PTNull:
+ return 1
+ case PTBool:
+ return 1 + 4
+ case PTInt, PTTime, PTFloat:
+ return 1 + 8
+ case PTGeoPoint:
+ return 1 + (8 * 2)
+ case PTString:
+ return 1 + int64(len(p.value.(string)))
+ case PTBlobKey:
+ return 1 + int64(len(p.value.(blobstore.Key)))
+ case PTBytes:
+ return 1 + int64(len(p.value.([]byte)))
+ case PTKey:
+ return 1 + p.value.(*Key).EstimateSize()
+ }
+ panic(fmt.Errorf("Unknown property type: %s", p.Type().String()))
+}
+
// MetaGetter is a subinterface of PropertyLoadSaver, but is also used to
// abstract the meta argument for RawInterface.GetMulti.
type MetaGetter interface {
@@ -767,6 +794,25 @@ func (pm PropertyMap) Problem() error {
return nil
}
+// EstimateSize estimates the size that it would take to encode this PropertyMap
+// in the production Appengine datastore. The calculation excludes metadata
+// fields in the map.
+//
+// It uses https://cloud.google.com/appengine/articles/storage_breakdown?csw=1
+// as a guide for sizes.
+func (pm PropertyMap) EstimateSize() int64 {
+ ret := int64(0)
+ for k, vals := range pm {
+ if !isMetaKey(k) {
+ ret += int64(len(k))
+ for i := range vals {
+ ret += vals[i].EstimateSize()
+ }
+ }
+ }
+ return ret
+}
+
func isMetaKey(k string) bool {
// empty counts as a metakey since it's not a valid data key, but it's
// not really a valid metakey either.
« no previous file with comments | « service/datastore/key.go ('k') | service/datastore/size_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698