Check for existing mapping when writing a custom applier in ConfORM - nhibernate

I am writing my first custom column name applier for ConfORM.
How do I check if another column has already been map with same mapping name?
This is what I have so far:
public class MyColumnNameApplier : IPatternApplier<PropertyPath, IPropertyMapper>
{
public bool Match(PropertyPath subject)
{
return (subject.LocalMember != null);
}
public void Apply(PropertyPath subject, IPropertyMapper applyTo)
{
string shortColumnName = ToOracleName(subject);
// How do I check if the short columnName already exists?
applyTo.Column(cm => cm.Name(shortColumnName));
}
private string ToOracleName(PropertyPath subject)
{
...
}
}
}
I need to shorten the generated column names to less than 30 characters to fit in with Oracle's 30 character limit. Because I am shortening the column names it is possible that the same column name can potentially be generated two different properties. I would like to know when a duplicate mapping occurs.
If I don't handle this scenario ConfORM/NHibernate allows two different properties to 'share' the same column name - this is obviously creates a problem for me.

if column names are mapped twice you will get exception about parameter count on first load. You can can check after configuring:
foreach (var clazz in config.ClassMappings)
{
var propertiesWithOneColumn = clazz.PropertyClosureIterator.Where(p => p.ColumnSpan == 1);
var distinctColumns = new HashSet<string>();
foreach (var prop in propertiesWithOneColumn)
{
var col = prop.ColumnIterator.First().Text;
if (distinctColumns.Add(col))
{
Console.WriteLine("duplicate column "+ col + " found for property " + prop.PersistentClass.ClassName + "." + prop.Name);
}
}
}

Related

RavenDB creating static index and query on dictionary

I'm trying to create a static index where I want all documents where a key exists and has a value. The value itself is not important, only the key exists.
I'm exploring this example with dynamic fields:
https://ravendb.net/docs/article-page/2.5/csharp/client-api/advanced/dynamic-fields
... and although I'm getting the index to work, I'm not sure if the query I'm using is correct.
This is the sample class:
public class Result
{
public Dictionary<string, List<Data>> Results { get; set; }
}
The key in the dictionary is the ID of a user (for example "user/1") and the value is a list of data-objects. The so the json-structure looks like this:
{
"Results" :
{
"user/1": [{...}],
"user/2": [{...}],
}
}
The index I use is this:
public class Result_ByUserId : AbstractIndexCreationTask<Result>
{
public Result_ByUserId()
{
Map = res => from r in res
select new
{
_ = r.Results
.Select(d => CreateField(d.Key, d.Value))
};
}
}
My problem comes down to the query, as it assumes I want to look at a specific key and value.
var resultat = session.Advanced.DocumentQuery<Result>("Result/ByUserId ")
.WhereEquals("user/1", "") // How do I write a !isNullOrEmpty?
.ToList();
... which I don't want to do. I only want the results that has a key in which the value is not null or empty. Does anybody have any good tips?
What you can do is index a boolean flag depending on if the dictionary has a value or not and then query on that.
public class Result_ByUserId : AbstractIndexCreationTask<Result>
{
public Result_ByUserId()
{
Map = res => from r in res
select new
{
_ = r.Results
.Select(d => CreateField(d.Key, d.Value != null ? true : false, false, true))
};
}
}
The query can then be:
var resultat = session.Advanced.DocumentQuery<Result>("Result/ByUserId ")
.WhereEquals("user/1", true)
.ToList();
This will return any Result documents that has a Dictionary with a key of user/1 and a dictionary value that's not null.
Not sure it's the best way of doing it, but it worked for me...
Hope this helps!

Pig - passing Databag to UDF constructor

I have a script which is loading some data about venues:
venues = LOAD 'venues_extended_2.csv' USING org.apache.pig.piggybank.storage.CSVLoader() AS (Name:chararray, Type:chararray, Latitude:double, Longitude:double, City:chararray, Country:chararray);
Then I want to create UDF which has a constructor that is accepting venues type.
So I tried to define this UDF like that:
DEFINE GenerateVenues org.gla.anton.udf.main.GenerateVenues(venues);
And here is the actual UDF:
public class GenerateVenues extends EvalFunc<Tuple> {
TupleFactory mTupleFactory = TupleFactory.getInstance();
BagFactory mBagFactory = BagFactory.getInstance();
private static final String ALLCHARS = "(.*)";
private ArrayList<String> venues;
private String regex;
public GenerateVenues(DataBag venuesBag) {
Iterator<Tuple> it = venuesBag.iterator();
venues = new ArrayList<String>((int) (venuesBag.size() + 1)); // possible fails!!!
String current = "";
regex = "";
while (it.hasNext()){
Tuple t = it.next();
try {
current = "(" + ALLCHARS + t.get(0) + ALLCHARS + ")";
venues.add((String) t.get(0));
} catch (ExecException e) {
throw new IllegalArgumentException("VenuesRegex: requires tuple with at least one value");
}
regex += current + (it.hasNext() ? "|" : "");
}
}
#Override
public Tuple exec(Tuple tuple) throws IOException {
// expect one string
if (tuple == null || tuple.size() != 2) {
throw new IllegalArgumentException(
"BagTupleExampleUDF: requires two input parameters.");
}
try {
String tweet = (String) tuple.get(0);
for (String venue: venues)
{
if (tweet.matches(ALLCHARS + venue + ALLCHARS))
{
Tuple output = mTupleFactory.newTuple(Collections.singletonList(venue));
return output;
}
}
return null;
} catch (Exception e) {
throw new IOException(
"BagTupleExampleUDF: caught exception processing input.", e);
}
}
}
When executed the script is firing error at the DEFINE part just before (venues);:
2013-12-19 04:28:06,072 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: <file script.pig, line 6, column 60> mismatched input 'venues' expecting RIGHT_PAREN
Obviously I'm doing something wrong, can you help me out figuring out what's wrong.
Is it the UDF that cannot accept the venues relation as a parameter. Or the relation is not represented by DataBag like this public GenerateVenues(DataBag venuesBag)?
Thanks!
PS I'm using Pig version 0.11.1.1.3.0.0-107.
As #WinnieNicklaus already said, you can only pass strings to UDF constructors.
Having said that, the solution to your problem is using distributed cache, you need to override public List<String> getCacheFiles() to return a list of filenames that will be made available via distributed cache. With that, you can read the file as a local file and build your table.
The downside is that Pig has no initialization function, so you have to implement something like
private void init() {
if (!this.initialized) {
// read table
}
}
and then call that as the first thing from exec.
You can't use a relation as a parameter in a UDF constructor. Only strings can be passed as arguments, and if they are really of another type, you will have to parse them out in the constructor.

LongListSelector Date Grouped Lists

Windows Phone 8 SDK question using the LongListSelector to group on dates.
I am familiar with the AlphaKeyGroup helper approach to grouping on letters.
Has anyone done/seen a similar write up for dates that is similarly locale aware? (Numbers would be a plus as well)
So I struggled with this one a bit too because the AlphaKeyGroup example from MSDN you mentioned is more complicated than it needs to be because of localization. What you are trying to do is create a new List object that that has one extra property, the Key. This Key property is the name that you group on. In the AlphaKeyGroup example it is each letter of the alphabet in your region. So create your own group object that inherits from List.
public class TimeKeyGroup<T> : List<T>
{
/// <summary>
/// The Key of this group.
/// </summary>
public string Key { get; private set; }
public TimeKeyGroup(string key)
{
Key = key;
}
}
Now create a method called CreateGroups that accepts an IEnumerable of the object you want to group and returns a list of you custom list object that you just created. In my implementation I was grouping Workout objects that had a TimeStamp property. In this method create group objects for each type of group key name you want such as "Last 7 Day" or "Last 6 Months". Then fill each group by loooping over the passed in IEnumerable group and evaluating each to determine where they should be grouped. Finally add each grouped list to a master group list and return it. Here is my method:
public static List<TimeKeyGroup<Workout>> CreateGroups(IEnumerable<Workout> workouts)
{
// Create List to hold each item
List<TimeKeyGroup<Workout>> groupedWorkouts = new List<TimeKeyGroup<Workout>>();
// Create a TimeKeyGroup for each group I want
TimeKeyGroup<Workout> LastSeven = new TimeKeyGroup<Workout>("Last Seven Days");
TimeKeyGroup<Workout> LastTwoWeeks = new TimeKeyGroup<Workout>("Last Two Weeks");
TimeKeyGroup<Workout> LastMonth = new TimeKeyGroup<Workout>("Last Month");
TimeKeyGroup<Workout> LastSixMonths = new TimeKeyGroup<Workout>("Last Six Months");
TimeKeyGroup<Workout> LastYear = new TimeKeyGroup<Workout>("Last Year");
TimeKeyGroup<Workout> AllTime = new TimeKeyGroup<Workout>("All Time");
// Fill each list with the appropriate workouts
foreach (Workout w in workouts)
{
if (w.TimeStamp > DateTime.Now.AddDays(-7))
{
LastSeven.Add(w);
continue;
}
else if (w.TimeStamp > DateTime.Now.AddDays(-14))
{
LastTwoWeeks.Add(w);
continue;
}
else if (w.TimeStamp > DateTime.Now.AddMonths(-1))
{
LastMonth.Add(w);
continue;
}
else if (w.TimeStamp > DateTime.Now.AddMonths(-6))
{
LastSixMonths.Add(w);
continue;
}
else if (w.TimeStamp > DateTime.Now.AddMonths(-12))
{
LastYear.Add(w);
continue;
}
else
{
AllTime.Add(w);
}
}
// Add each TimeKeyGroup to the overall list
groupedWorkouts.Add(LastSeven);
groupedWorkouts.Add(LastTwoWeeks);
groupedWorkouts.Add(LastMonth);
groupedWorkouts.Add(LastSixMonths);
groupedWorkouts.Add(LastYear);
groupedWorkouts.Add(AllTime);
return groupedWorkouts;
}
Now you have a nice list of grouped lists. Awesome! The rest is just hooking the itemssource property of your LongListSelector to this new list and defining a JumpListStyle and GroupedHeaderTemplate. The original article you referenced has all that info.
Good luck and happy Windows Phone Development!
I've had success with this example from MSDN after I was stuck on the same example as you are now. The Group.cs file contains an implementation of a group which can be freely used with strings. My guess is, that you could easily add another property of DateTime and then you could try grouping by dates.
Well I use a modified version of the AlphaKeyGroup. This new class I called StringKeyGroup and creates the groups based on the first charachter of the items. So it's simply a matter of replacing AlphaKeyGroup with StringKeyGroup.
This new functionality can be used like:
myLonglistSelector.ItemSource = GroupedItems(myCollection);
....
public ObservableCollection<StringKeyGroup<myClass>> GroupedItems(IEnumerable<myClass> source)
{
return StringKeyGroup<myClass>.CreateGroups(source,
System.Threading.Thread.CurrentThread.CurrentUICulture,
s => s.Name, true);
}
Here's the code for StringKeyGroup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Globalization;
namespace MyNameSpace
{
public class StringKeyGroup<T> : ObservableCollection<T>
{
public delegate string GetKeyDelegate(T item);
public string Key { get; private set; }
public StringKeyGroup(string key)
{
Key = key;
}
public static ObservableCollection<StringKeyGroup<T>> CreateGroups(IEnumerable<T> items, CultureInfo ci, GetKeyDelegate getKey, bool sort)
{
var list = new ObservableCollection<StringKeyGroup<T>>();
foreach (var item in items)
{
var itemKey = getKey(item).Substring(0, 1).ToLower();
var itemGroup = list.FirstOrDefault(li => li.Key == itemKey);
var itemGroupIndex = itemGroup != null ? list.IndexOf(itemGroup) : -1 ;
if (itemGroupIndex == -1)
{
list.Add(new StringKeyGroup<T>(itemKey));
itemGroupIndex = list.Count - 1;
}
if (itemGroupIndex >= 0 && itemGroupIndex < list.Count)
{
list[itemGroupIndex].Add(item);
}
}
if (sort)
{
foreach (var group in list)
{
group.ToList().Sort((c0, c1) => ci.CompareInfo.Compare(getKey(c0), getKey(c1)));
}
}
return list;
}
}
}
To use LongListSelector with numbers, lets try to group a list of people by age, instead of by the first letter of their first name (in the MSDN PeopleHub example)
They use the mysterious AlphaKeyGroup, which is a list of people with first names starting with the same letter (That letter becomes the Key of the AlphaKeyGroup). AlphaKeyGroups of people can look like this:
Key = A: Amber, Amelia (where both People objects have names that start with A)
Key = B: Bob, Brian (where both People objects have names that start with B)
Key = C: etc etc
We're gonna use the IntegerKeyGroup, which is a list of people who have the same age. IntegerKeyGroups of people can look like this:
Key = 23: Jennifer, Ryan (where both People objects are 23 years old)
Key = 26: Amber, Megan (where both People objects are 26 years old)
Key = 34: etc etc
So to follow GentryRiggen's framework, we have to first define the IntegerKeyGroup, then stick people in the age groups they belong. I put these in ViewModel file.
public class IntegerKeyGroup<T> : List<T>
{
public int Key { get; private set; }
public IntegerKeyGroup(int key)
{
Key = key;
}
}
Notice IntegerKeyGroup is simply a List but with a special integer member called Key. This means we can label a list of people with an age integer as the Key.
Now we need to sort our big list of unsorted people into IntegerKeyGroups of different ages, and finally combine all these IntegerKeyGroups together. This combined list of IntegerKeyGroups is what LongListSelector accepts to be displayed.
public static List<IntegerKeyGroup<Person>> CreateGroups(IEnumerable<Person> UnsortedPeopleList)
{
// Create combined list of IntegerKeyGroups
List<IntegerKeyGroup<Person>> CombinedPeopleList = new List<IntegerKeyGroup<Person>>();
// Create a IntegerKeyGroup for each age group I want,
// The constructor parameters sets the Key to the IntegerKeyGroup
IntegerKeyGroup<Person> Age23s = new IntegerKeyGroup<Person>(23);
IntegerKeyGroup<Person> Age26s = new IntegerKeyGroup<Person>(26);
IntegerKeyGroup<Person> Age34s = new IntegerKeyGroup<Person>(34);
// Populate each IntegerKeyGroup with the appropriate Persons
foreach (Person p in UnsortedPeopleList)
{
switch (p.Age)
{
case 23: Age23s.Add(p); continue;
case 26: Age26s.Add(p); continue;
case 34: Age34s.Add(p); continue;
default: continue; // we don't support ages other than the 3 above
}
}
// Add each IntegerKeyGroup to the overall list
CombinedPeopleList.Add(Age23s);
CombinedPeopleList.Add(Age26s);
CombinedPeopleList.Add(Age34s);
return CombinedPeopleList;
}
Still in the ViewModel file, make the List of IntegerKeyGroups publicly accessible with the CreateGroups function.
public List<IntegerKeyGroup<Person>> AgeGroupedPeople
{
get
{
return CreateGroups(UnsortedPeople);
}
}
Now in the XAML, make 1 change to the original code from the MSDN PeopleHub example:
<phone:LongListSelector Name="peopleLongListSelector"
ItemsSource="{Binding AgeGroupedPeople}" <!-- Change is in this line! -->
JumpListStyle="{StaticResource LongListSelectorJumpListStyle}"
ListHeaderTemplate="{StaticResource LongListSelectorHeaderTemplate}"
GroupHeaderTemplate="{StaticResource LongListSelectorGroupHeaderTemmplate}"
ItemTemplate="{StaticResource LongListSelectorItemTemplate}"
HideEmptyGroups ="true" IsGroupingEnabled ="true" LayoutMode="List">
</phone:LongListSelector>
This should group people by integers, in this case, age.

RavenDB adds integer to ID when ends with a slash

I use RavenDB 1.0.0.426
I just experienced a weird scenario when importing data from an external source into RavenDB:
I chose to use the same unique ID as the external source uses, prefixed with a certain string. But. When I store a document with an ID that ends with a '/', raven automatically adds a number to the end of the ID, causing the document to NOT overwrite existing document stored with the same id.
I have recreated a simple scenario to cause the error:
The type I save:
public class Foo
{
public string Id { get; set; }
public Foo(string id)
{
Id = id;
}
}
Method saving a doc with the same id 10 times and afterwards checks the document count:
public void RunTest(string id)
{
for (int i = 0; i < 10; i++)
{
using (var doc = new DocumentStore() { Url = "http://pc-009:8080/" })
{
doc.Initialize();
using (var session = doc.OpenSession())
{
session.Store(new Foo(id));
session.SaveChanges();
}
}
}
// Wait for the data to be persisted
Thread.Sleep(2000);
using (var doc = new DocumentStore() { Url = "http://pc-009:8080/" })
{
doc.Initialize();
using (var session = doc.OpenSession())
{
var foos = session.Query<Foo>();
int fooCount = foos.Count();
// ASSERT HERE THAT fooCount is 1
}
}
}
Running the method with "id1" successfully overwrites existing documents:
RunTest("id1"); // Works fine
Running method with "id1/" ends up creating 10 documents in the database:
RunTest("id1/"); // Results in multiple documents to be created
I know that if you do not define your own ID, raven will autogenerate doc-ids on the fly, but is the behavior I describe above by design?
Thankyou in advance
Stephan,
This is expected, when your key ends with /, it asks ravendb to use identity generation strategy.
See the docs here:
http://ravendb.net/documentation/docs-api-key-generation
If you want a key that ends with /, you can url encode the keys

Generate parameter list with userdefined types at runtime (using C#)

As part of my project, I am trying to build a web UI where user will select a method and pass the values. My program should be able to call the method dynamically and build a parameter list on runtime to pass it to the method.
I have created a comma separated list (string) of key and value pairs. This key/value pair is nothing but the parameter name and value of my method (methodname stored in a variable). Example: string params = "ID:123;Name:Garry;Address:addressObject;AddressLine:108 Plaza Lane;City:Avenel;State:NJ;Zip:07001;". Where ID and Name are simple string varaibles while Address is user defined type. What follows after Address i.e. AddressLine, City, State and Zip is elements of Address object. And my method definition is
public string GetInfo(string ID, string Name, Address addressObject)
{
//return something;
}
I am dynamically calling the method (GetInfo) that is stored in sMethodName variable using DynamicProxy like :
string sMethodName = "GetInfo";
object result = (object) proxy.CallMethod(sMethodName, arguments);
Challenge is how to pass the argument list dynamically? Till now I am just able to extract the values from the csv variable into NamedValueCollection. Here is the code:
public static void StoreParameterValues(string param)
{
nvc = new NameValueCollection();
param = param.TrimEnd(';');
string[] parameters = param.Split(new char[] { ';' });
foreach (string val in parameters)
{
string[] keyValue = val.Split(new char[] { ':' });
nvc.Add(keyValue[0], keyValue[1]);
}
}
..and here is the code that tries to build the parameter:
string methodName = "GetInfo";
DynamicProxyFactory factory = new DynamicProxyFactory("http://../myservice.svc");
string sContract = "";
foreach (ServiceEndpoint endpoint in factory.Endpoints)
{
sContract = endpoint.Contract.Name;
}
DynamicProxy proxy = factory.CreateProxy(sContract);
string[] values = null;
// Create the parameter list
object[] arguments = new object[nvc.Count];
int i = -1;
foreach (string key in nvc.Keys)
{
values = nvc.GetValues(key);
foreach (string value in values)
{
arguments[++i] = value;
}
}
object result = (object) proxy.CallMethod(methodName, arguments);
The above code works if I have simple primitive types but not sure how can I build the logic for any other userdefined types. How can I create a object dynamically of type stored in a variable? Not sure if I was able to put my question correctly. I hope so :)
Edit: 01/19/2011: Applied the suggestion from Chris - using Reflection instead of ComponentModel.
I have converted the code to make it more generic. This works now for all primitive and custom types (resursion). Code snippet below:
private object BuildParameterList(Type type)
{
object item = new object();
item = Activator.CreateInstance(type);
PropertyInfo[] propArray = type.GetProperties(BindingFlags.Public|BindingFlags.Instance);
for (int i = 0; i < propArray.Length; i++)
{
PropertyInfo pi = (PropertyInfo)propArray[i];
////Check for custom type
if (IsCustomType(pi.PropertyType))
{
object item1 = BuildParameterList(pi.PropertyType);
pi.SetValue(item, item1, null);
}
else
{
if (property.ContainsKey(pi.Name))
{
pi.SetValue(item, Convert.ChangeType(property[pi.Name], pi.PropertyType), null);
}
}
}
return item;
}
But if one of the property is Color (I just tested with Color type, will fail with other system types aswell-i guess), then it fails at the following line. Not sure how to handle system types - Color or something similar.
pi.SetValue(item, Convert.ChangeType(property[pi.Name], pi.PropertyType), null);
Can you not find what types are expected by the method, by inspecting its ParameterInfos:
endpoint.Contract.ContractType.GetMethod(methodName).GetParameters();
and then instantiating the custom types using:
Activator.CreateInstance(parameterType);