Skip to main content

Plugins: Compilation Extensions

In certain situations users may want to use more complex logic to calculate a value of an index entry field. To do this we have introduced an AbstractDynamicCompilationExtensionto RavenDB.

public abstract class AbstractDynamicCompilationExtension
{
public abstract string[] GetNamespacesToImport();

public abstract string[] GetAssembliesToReference();
}

where:

  • GetNamespacesToImport returns a list of namespaces that RavenDB will have to import
  • GetAssembliesToReference returns a list of full paths to assemblies

Example - Check if a given word is a palindrome

public static class Palindrome
{
public static bool IsPalindrome(string word)
{
if (string.IsNullOrEmpty(word))
return true;

int min = 0;
int max = word.Length - 1;
while (true)
{
if (min > max)
return true;

char a = word[min];
char b = word[max];
if (char.ToLower(a) != char.ToLower(b))
return false;

min++;
max--;
}
}
}
public class PalindromeDynamicCompilationExtension : AbstractDynamicCompilationExtension
{
public override string[] GetNamespacesToImport()
{
return new[]
{
typeof (Palindrome).Namespace
};
}

public override string[] GetAssembliesToReference()
{
return new[]
{
typeof (Palindrome).Assembly.Location
};
}
}

Now we can use our Palindrome in our index definition.

store.DatabaseCommands.PutIndex("Dictionary/Palindromes", new IndexDefinition
{
Map = @"from word in docs.Words
select new
{
Word = word.Value,
IsPalindrome = Palindrome.IsPalindrome(word.Value)
}"
});