Files
spark/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowHasher.cs
T
2023-09-04 16:25:40 -04:00

104 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BCG
{
public class BlueRowHasher
{
private int _n_keys;
private int _hash_counter = 0;
private bool _thread_safe = false;
private IDictionary<object, object> _root_map = new Dictionary<object, object>();
public BlueRowHasher(int n_keys, bool thread_safe = false)
{
_n_keys = n_keys;
_thread_safe = thread_safe;
}
public string get_string_hash(IBlueRow row, int[] key_cols)
{
return get_hash(row, key_cols).ToString();
}
/// <summary>
/// gets the hash for a row with given keys,
/// if key does not exist is adds it adds it
/// and returns the hash
/// </summary>
public int get_hash(IBlueRow row, int[] key_cols)
{
IDictionary<object, object> d = _root_map;
for (int i = 0; ; i++)
{
object v = row[key_cols[i]];
if (i < _n_keys - 1)
{
object inner_d = null;
if (!d.TryGetValue(v, out inner_d))
{
inner_d = _get_new_dictionary();
d.Add(v, inner_d);
}
d = (IDictionary<object, object>)inner_d;
}
else
{
object rv;
if (!d.TryGetValue (v, out rv))
{
rv = _get_next_hash();
d.Add(v, rv);
}
return (int)rv;
}
}
}
public int get_hash(params object[] key_vals)
{
IDictionary<object, object> d = _root_map;
for (int i = 0; ; i++)
{
object v = key_vals[i];
if (i < _n_keys - 1)
{
object inner_d = null;
if (!d.TryGetValue(v, out inner_d))
{
inner_d = _get_new_dictionary();
d.Add(v, inner_d);
}
d = (IDictionary<object, object>)inner_d;
}
else
{
object rv;
if (!d.TryGetValue(v, out rv))
{
rv = _get_next_hash();
d.Add(v, rv);
}
return (int)rv;
}
}
}
private int _get_next_hash()
{
return _thread_safe ? System.Threading.Interlocked.Increment(ref _hash_counter) : ++_hash_counter;
}
private IDictionary<object, object> _get_new_dictionary()
{
if (_thread_safe)
return new System.Collections.Concurrent.ConcurrentDictionary<object, object>();
else
return new Dictionary<object, object>();
}
}
}