1 /* 2 * Hunt - A redis client library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.redis.util.RedisClusterHashTagUtil; 13 14 import hunt.redis.util.SafeEncoder; 15 16 import hunt.Exceptions; 17 18 import std.array; 19 import std.range; 20 import std.string; 21 22 /** 23 * Holds various methods/utilities to manipualte and parse redis hash-tags. See <a 24 * href="http://redis.io/topics/cluster-spec">Cluster-Spec : Keys hash tags</a> 25 */ 26 class RedisClusterHashTagUtil { 27 28 private this() { 29 throw new InstantiationError("Must not instantiate this class"); 30 } 31 32 static string getHashTag(string key) { 33 return extractHashTag(key, true); 34 } 35 36 static bool isClusterCompliantMatchPattern(const(ubyte)[] matchPattern) { 37 return isClusterCompliantMatchPattern(SafeEncoder.encode(matchPattern)); 38 } 39 40 static bool isClusterCompliantMatchPattern(string matchPattern) { 41 string tag = extractHashTag(matchPattern, false); 42 return !tag.empty(); 43 } 44 45 private static string extractHashTag(string key, bool returnKeyOnAbsence) { 46 ptrdiff_t s = key.indexOf("{"); 47 if (s > -1) { 48 ptrdiff_t e = key.indexOf("}", s + 1); 49 if (e > -1 && e != s + 1) { 50 return key[s + 1 .. e]; 51 } 52 } 53 return returnKeyOnAbsence ? key : null; 54 } 55 }