2 using System.Collections.Generic;
7 internal sealed
class WorkerQueue<T>
9 public System.Uri url {
get;
private set; }
11 private bool has_primary_key;
12 private bool update_on_existing_pk;
13 private IList<T> queue;
14 private Dictionary<RecordKey, int> primary_key_map;
21 public WorkerQueue(
System.Uri url )
25 this.has_primary_key =
false;
26 this.update_on_existing_pk =
false;
28 queue =
new List<T>();
40 public WorkerQueue(
System.Uri url,
int capacity,
bool has_primary_key,
bool update_on_existing_pk)
43 this.capacity = capacity;
44 this.has_primary_key = has_primary_key;
45 this.update_on_existing_pk = update_on_existing_pk;
47 queue =
new List<T>();
51 if (this.has_primary_key)
52 primary_key_map =
new Dictionary<RecordKey, int>((int)Math.Round(
this.capacity / 0.75));
61 public IList<T> flush()
63 IList<T> old_queue = this.queue;
64 queue =
new List<T>(this.capacity);
67 if (this.primary_key_map != null)
68 this.primary_key_map.Clear();
82 public IList<T> insert(T record, RecordKey key)
84 if (this.has_primary_key && key.isValid())
87 if (this.update_on_existing_pk)
91 if (this.primary_key_map.TryGetValue(key, out key_idx))
94 this.queue[key_idx] = record;
98 this.queue.Add(record);
99 this.primary_key_map.Add(key, (this.queue.Count - 1));
104 if (this.primary_key_map.ContainsKey(key))
109 this.queue.Add(record);
110 this.primary_key_map.Add(key, (this.queue.Count - 1));
119 if (queue.Count == capacity)