code: func (s *Store) kvsLockTxn(tx *memdb.Txn, idx uint64, entry *structs.DirEntry) (bool, error) {
  	// Verify that a session is present.
  	if entry.Session == "" {
  		return false, fmt.Errorf("missing session")
  	}
  
  	// Verify that the session exists.
  	sess, err := tx.First("sessions", "id", entry.Session)
  	if err != nil {
  		return false, fmt.Errorf("failed session lookup: %s", err)
  	}
  	if sess == nil {
  		return false, fmt.Errorf("invalid session %#v", entry.Session)
  	}
  
  	// Retrieve the existing entry.
  	existing, err := tx.First("kvs", "id", entry.Key)
  	if err != nil {
  		return false, fmt.Errorf("failed kvs lookup: %s", err)
  	}
  
  	// Set up the entry, using the existing entry if present.
  	if existing != nil {
  		e := existing.(*structs.DirEntry)
  		if e.Session == entry.Session {
  			// We already hold this lock, good to go.
  			entry.CreateIndex = e.CreateIndex
  			entry.LockIndex = e.LockIndex
  		} else if e.Session != "" {
  			// Bail out, someone else holds this lock.
  			return false, nil
  		} else {
  			// Set up a new lock with this session.
  			entry.CreateIndex = e.CreateIndex
  			entry.LockIndex = e.LockIndex + 1
  		}
  	} else {
  		entry.CreateIndex = idx
  		entry.LockIndex = 1
  	}
  	entry.ModifyIndex = idx
  
  	// If we made it this far, we should perform the set.
  	if err := s.kvsSetTxn(tx, idx, entry, true); err != nil {
  		return false, err
  	}
  	return true, nil
  }
docs: // kvsLockTxn is the inner method that does a lock inside an existing
  // transaction.