Addressing review comments
Change-Id: If641fe4856010830532f49480d90d18294997a9b
diff --git a/syncbase/key.go b/syncbase/key.go
index 362631c..e11dacf 100644
--- a/syncbase/key.go
+++ b/syncbase/key.go
@@ -31,56 +31,61 @@
base64Encoding = base64.NewEncoding(uuidCharacterSet).WithPadding(base64.NoPadding)
}
-// Resize string 's' to 'length'. The string will be padded with the first character from the
-// uuidCharacterSet if it is too short, and cut off if its too long.
-func resize(s string, length int) string {
- switch {
- case len(s) > length:
+// Truncates string 's' to 'length'. Will remove characters from the beginning of the string
+// if 's' is too long.
+func truncate(s string, length int) string {
+ if len(s) > length {
return s[len(s)-length:]
- case len(s) == length:
- return s
- default:
- for len(s) < length {
- return uuidCharacterSet[:1] + s
- }
+ } else {
return s
}
}
+// Pad string 's' to 'length'. Will prefix 's' with sequences of 'padding' if the string
+// is too short. If 'padding' has more than one character, the resulting string may be
+// longer than 'length'.
+func pad(s string, length int, padding string) string {
+ for len(s) < length {
+ s = padding + s
+ }
+
+ return s
+}
+
// UUID generates a sequential and pseudo-random key that can be used as an ID within
-// Syncbase. The generates IDs are of the format [a-zA-z][a-zA-Z0-9$_]{19}
+// Syncbase. The generated IDs are of the format [a-zA-z][a-zA-Z0-9$_]{19}
//
// A UUID is made up of four parts:
-// - a prefix which gurantees that the generated UUID is a valid variable name in most programming
+// - a prefix which guarantees that the generated UUID is a valid variable name in most programming
// languages
// - a time component which uses nanosecond precision
-// - a counter component which is used to gurantee ordering between calls
-// - a random component which achieves pseude-randomness
+// - a counter component which is used to guarantee ordering between calls
+// - a random component which achieves pseudo-randomness
func UUID() string {
buffer := new(bytes.Buffer)
- // Encode the current time in nanos as an base64-charater string.
+ // Encode the current time in nanoseconds as a base64-character string.
// The maximum encoded length is 11 characters: 2^63/64^11 < 1
currentTime := time.Now().UnixNano()
binary.Write(buffer, binary.BigEndian, currentTime)
uuidTime := base64Encoding.EncodeToString(buffer.Bytes())
- // Increment the global count of invocations and encde as a 3-character base64 string.
+ // Increment the global count of invocations and encode as a 3-character base64-string.
// Note that overflows here are fine.
buffer.Reset()
currentInvocation := atomic.AddUint32(&invocationCounter, 1)
binary.Write(buffer, binary.BigEndian, currentInvocation)
uuidInvocation := base64Encoding.EncodeToString(buffer.Next(4)[1:])
- // Encode 4 bytes of randomness as base64-string (maximum of 6 characters)
+ // Encode 4 bytes of randomness as a base64-string (maximum of 6 characters)
random := make([]byte, 4)
rand.Read(random)
uuidRandNumber := base64Encoding.EncodeToString(random)
return fmt.Sprintf(
"%s%s%s%s",
- resize(uuidPrefix, 1),
- resize(uuidTime, 11),
- resize(uuidInvocation, 2),
- resize(uuidRandNumber, 6))
+ uuidPrefix,
+ pad(uuidTime, 11, uuidCharacterSet[:1]),
+ pad(truncate(uuidInvocation, 2), 2, uuidCharacterSet[:1]),
+ pad(uuidRandNumber, 6, uuidCharacterSet[:1]))
}