private void ExecuteOnReduceKey(string view, string reduceKey,
Table table,
IDictionary<string, JET_COLUMNID> columnids,
Action updateAction,
Action insertAction)
{
var hashReduceKey = HashReduceKey(reduceKey);
Api.JetSetCurrentIndex(session, table, "by_view_and_hashed_reduce_key");
Api.MakeKey(session, table, view, Encoding.Unicode, MakeKeyGrbit.NewKey);
Api.MakeKey(session, table, hashReduceKey, MakeKeyGrbit.None);
Api.MakeKey(session, table, reduceKey, Encoding.Unicode, MakeKeyGrbit.None);
if (Api.TrySeek(session, table, SeekGrbit.SeekEQ) == false)
{
if (insertAction == null)
return;
using (var update = new Update(session, table, JET_prep.Insert))
{
Api.SetColumn(session, table, columnids["view"], view, Encoding.Unicode);
Api.SetColumn(session, table, columnids["reduce_key"], reduceKey, Encoding.Unicode);
Api.SetColumn(session, table, columnids["hashed_reduce_key"], hashReduceKey);
insertAction();
update.SaveAndGotoBookmark();
}
return;
}
Api.MakeKey(session, table, view, Encoding.Unicode, MakeKeyGrbit.NewKey);
Api.MakeKey(session, table, hashReduceKey, MakeKeyGrbit.None);
Api.MakeKey(session, table, reduceKey, Encoding.Unicode, MakeKeyGrbit.None);
Api.TrySetIndexRange(session, table, SetIndexRangeGrbit.RangeInclusive | SetIndexRangeGrbit.RangeUpperLimit);
do
{
var reduceKeyFromDb = Api.RetrieveColumnAsString(session, table, columnids["reduce_key"]);
if (StringComparer.Ordinal.Equals(reduceKey, reduceKeyFromDb) == false)
continue;
updateAction();
return;
} while (Api.TryMoveNext(session, table));
// couldn't find it...
if (insertAction == null)
return;
using (var update = new Update(session, table, JET_prep.Insert))
{
Api.SetColumn(session, table, columnids["view"], view, Encoding.Unicode);
Api.SetColumn(session, table, columnids["reduce_key"], reduceKey, Encoding.Unicode);
Api.SetColumn(session, table, columnids["hashed_reduce_key"], hashReduceKey);
insertAction();
update.SaveAndGotoBookmark();
}
}