public FindStartsWith ( string indexName, byte lookupValue ) : byte[]>>.IEnumerable |
||
indexName | string | |
lookupValue | byte | |
return | byte[]>>.IEnumerable |
public IEnumerable<KeyValuePair<byte[], byte[]>> FindStartsWith(string indexName, byte[] lookupValue) {
KeyValueStore indexStore = GetSecondaryIndex(indexName);
// Loop over the values
foreach (var pair in indexStore.EnumerateFromKey(lookupValue)) {
var key = pair.Key;
var value = pair.Value;
// construct our index key pattern (lookupvalue | key)
if (ByteArray.CompareMemCmp(key, 0, lookupValue, 0, lookupValue.Length) == 0) {
int offset = 0;
byte[] objectKey = null;
if (Manifest.RazorFormatVersion < 2) {
if (ByteArray.CompareMemCmp(key, key.Length - value.Length, value, 0, value.Length) == 0)
objectKey = pair.Value;
} else {
int indexKeyLen = Helper.Decode7BitInt(pair.Value, ref offset);
if (lookupValue.Length <= indexKeyLen) {
objectKey = ItemKeyFromIndex(pair, indexKeyLen);
}
}
if (objectKey != null) {
var primaryValue = Get(objectKey);
if (primaryValue != null)
yield return new KeyValuePair<byte[], byte[]>(objectKey, primaryValue);
}
} else {
// if the above condition was not met then we must have enumerated past the end of the indexed value
yield break;
}
}
}
public void FindStartsWith() { string path = Path.GetFullPath("TestData\\FindStartsWith"); using (var db = new KeyValueStore(path)) { db.Truncate(); var indexed = new SortedDictionary<string, byte[]>(); indexed["Bytes"] = Encoding.UTF8.GetBytes("112"); db.Set(BitConverter.GetBytes(112), Encoding.UTF8.GetBytes("112"), indexed); indexed["Bytes"] = Encoding.UTF8.GetBytes("1123"); db.Set(BitConverter.GetBytes(1123), Encoding.UTF8.GetBytes("1123"), indexed); indexed["Bytes"] = Encoding.UTF8.GetBytes("11235"); db.Set(BitConverter.GetBytes(11235), Encoding.UTF8.GetBytes("11235"), indexed); indexed["Bytes"] = Encoding.UTF8.GetBytes("112358"); db.Set(BitConverter.GetBytes(112358), Encoding.UTF8.GetBytes("112358"), indexed); } using (var db = new KeyValueStore(path)) { var exact = db.Find("Bytes", Encoding.UTF8.GetBytes("1123")).ToList(); Assert.AreEqual(1, exact.Count()); Assert.AreEqual("1123", Encoding.UTF8.GetString(exact[0].Value)); var startsWith = db.FindStartsWith("Bytes", Encoding.UTF8.GetBytes("1123")).ToList(); Assert.AreEqual(3, startsWith.Count()); Assert.AreEqual("112358", Encoding.UTF8.GetString(startsWith[0].Value)); Assert.AreEqual("11235", Encoding.UTF8.GetString(startsWith[1].Value)); Assert.AreEqual("1123", Encoding.UTF8.GetString(startsWith[2].Value)); } }