Brunet.Services.Dht.TableServer.PutHandler C# (CSharp) Метод

PutHandler() публичный Метод

Attempts to store the key:value pair into this server.
First the dht deletes any expired entries stored at the key, second it retrieves the entries from the data store. If it is empty it creates a new entry and returns. Otherwise, it looks for the value in the list and updates the lease time. If there is no entry for that key:value pair it either adds it in the case of a put or throws an exception if it is a create.
Data is too large, unresolved remote issues, or the create is no successful
public PutHandler ( MemBlock key, MemBlock value, int ttl, bool unique ) : bool
key MemBlock The index to store the data at.
value MemBlock Data to store at the key.
ttl int Dht lease time in seconds
unique bool True if this should perform a create, false otherwise.
Результат bool
    public bool PutHandler(MemBlock key, MemBlock value, int ttl, bool unique) {
      DateTime create_time = DateTime.UtcNow;
      DateTime end_time = create_time.AddSeconds(ttl);

      lock(_sync) {
        _data.DeleteExpired(key);
        LinkedList<Entry> data = _data.GetEntries(key);
        if(data != null) {
          foreach(Entry ent in data) {
            if(ent.Value.Equals(value)) {
              if(end_time > ent.EndTime) {
                _data.UpdateEntry(ent.Key, ent.Value, end_time);
              }
              return true;
            }
          }
          // If this is a create we didn't find an previous entry, so failure, else add it
          if(unique) {
            throw new Exception("ENTRY_ALREADY_EXISTS");
          }
        }

        // This is either a new key or a new value (put only)
        Entry e = new Entry(key, value, create_time, end_time);
        _data.AddEntry(e);
      } // end of lock
      return true;
    }