github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/store/node_extern.go (about)

     1  package store
     2  
     3  import (
     4  	"sort"
     5  	"time"
     6  )
     7  
     8  // NodeExtern is the external representation of the
     9  // internal node with additional fields
    10  // PrevValue is the previous value of the node
    11  // TTL is time to live in second
    12  type NodeExtern struct {
    13  	Key           string      `json:"key,omitempty"`
    14  	Value         *string     `json:"value,omitempty"`
    15  	Dir           bool        `json:"dir,omitempty"`
    16  	Expiration    *time.Time  `json:"expiration,omitempty"`
    17  	TTL           int64       `json:"ttl,omitempty"`
    18  	Nodes         NodeExterns `json:"nodes,omitempty"`
    19  	ModifiedIndex uint64      `json:"modifiedIndex,omitempty"`
    20  	CreatedIndex  uint64      `json:"createdIndex,omitempty"`
    21  }
    22  
    23  func (eNode *NodeExtern) loadInternalNode(n *node, recursive, sorted bool) {
    24  	if n.IsDir() { // node is a directory
    25  		eNode.Dir = true
    26  
    27  		children, _ := n.List()
    28  		eNode.Nodes = make(NodeExterns, len(children))
    29  
    30  		// we do not use the index in the children slice directly
    31  		// we need to skip the hidden one
    32  		i := 0
    33  
    34  		for _, child := range children {
    35  			if child.IsHidden() { // get will not return hidden nodes
    36  				continue
    37  			}
    38  
    39  			eNode.Nodes[i] = child.Repr(recursive, sorted)
    40  			i++
    41  		}
    42  
    43  		// eliminate hidden nodes
    44  		eNode.Nodes = eNode.Nodes[:i]
    45  
    46  		if sorted {
    47  			sort.Sort(eNode.Nodes)
    48  		}
    49  
    50  	} else { // node is a file
    51  		value, _ := n.Read()
    52  		eNode.Value = &value
    53  	}
    54  
    55  	eNode.Expiration, eNode.TTL = n.ExpirationAndTTL()
    56  }
    57  
    58  type NodeExterns []*NodeExtern
    59  
    60  // interfaces for sorting
    61  func (ns NodeExterns) Len() int {
    62  	return len(ns)
    63  }
    64  
    65  func (ns NodeExterns) Less(i, j int) bool {
    66  	return ns[i].Key < ns[j].Key
    67  }
    68  
    69  func (ns NodeExterns) Swap(i, j int) {
    70  	ns[i], ns[j] = ns[j], ns[i]
    71  }