public ValueListImpl Read(KeyImpl key) { KeyValuePair <TimestampLog, ValueListImpl> val; Boolean retry = true; foreach (KeyValueBaseSlaveClient s in slaves.OrderByDescending(a => Guid.NewGuid())) { val = s.Read(key); if (LastLSN.IsBefore(val.Key)) { LastLSN = val.Key; retry = false; break; } } if (retry && masterIsLive) { try { val = master.Read(key); } catch (TimeoutException) { masterIsLive = false; } } return(val.Value); }
public IEnumerable <ValueListImpl> Scan(KeyImpl begin, KeyImpl end, IPredicate <ValueListImpl> predicate) { KeyValuePair <TimestampLog, IEnumerable <ValueListImpl> > val; Boolean retry = true; foreach (KeyValueBaseSlaveClient s in slaves.OrderByDescending(a => Guid.NewGuid())) { try { val = s.Scan(begin, end, predicate); } catch (TimeoutException) { slaves.Remove(s); // Remove slave on timeout } if (LastLSN.IsBefore(val.Key)) { LastLSN = val.Key; retry = false; break; } } if (retry) { val = master.Scan(begin, end, predicate); } return(val.Value); }
public LogRecord(OperationType operationType, object[] parameters) { this.operationType = operationType; this.parameters = parameters; lock (LogRecord.lastTimestamp) { this.lsn = LogRecord.lastTimestamp.Increment(); } }
public void Update(KeyImpl key, TimestampLog lsn, ValueListImpl newValues) { Object o; if (!locks.TryGetValue(key, out o)) { throw new KeyNotFoundException <KeyImpl>(key); } lock (o) { this.Remove(key); this.Insert(key, lsn, newValues); } }
public void Insert(KeyImpl key, TimestampLog lsn, ValueListImpl values) { Object o = new Object(); if (!locks.TryAdd(key, o)) { throw new KeyAlreadyPresentException <KeyImpl>(key); } lock (o) { byte[] data = serializer.ToByteArray(values); appleStore.Write(nextFree, data); dict[key] = new AllocRecord(nextFree, data.Length); timestamps[key] = lsn; nextFree += data.Length; return; } }
// Assumes that all values of same key are on consecutive lines. private void LoadIndexFromFile(string serverFilename) { int? lastKey = null; List <int> values = new List <int>(); LogRecord lr = new LogRecord(OperationType.Insert, null); stamp = lr.Lsn; using (StreamReader sr = new StreamReader(serverFilename)) { string line; while ((line = sr.ReadLine()) != null) { if (line.First(c => !whiteSpace.Contains(c)) == '#') { continue; } string[] parts = line.Split(whiteSpace, StringSplitOptions.RemoveEmptyEntries); int key = Int32.Parse(parts[0], CultureInfo.InvariantCulture); int value = Int32.Parse(parts[1], CultureInfo.InvariantCulture); if (!lastKey.HasValue) { lastKey = key; } else if (key != lastKey) { index.Insert(new KeyImpl(lastKey.Value), new ValueListImpl(values.Select(v => new ValueImpl(v)))); values.Clear(); lastKey = key; } values.Add(value); } } }
public void BulkPut(IEnumerable <KeyValuePair <KeyImpl, ValueListImpl> > pairs, TimestampLog lsn) { lock (dict) { foreach (KeyValuePair <KeyImpl, ValueListImpl> pair in pairs) { if (dict.ContainsKey(pair.Key)) { Update(pair.Key, lsn, pair.Value); } else { Insert(pair.Key, lsn, pair.Value); } } } }