title: Secondary Index on Key/Value database
body: <p>Lets say, I have  data structure like </p>
  
  <pre><code> type User struct {
        UUid string 
        Username string
        Email String 
        Password string 
        FirstName string 
        LastName string
  }
  </code></pre>
  
  <p>I am storing Users []User into a key/value database in levelDB. The unique key will be UUid and then user struct will be endoed and stored against this UUID.</p>
  
  <pre><code>var network bytes.Buffer // Stand-in for a network connection
  enc := gob.NewEncoder(&amp;network)
  err := enc.Encode(user)
     if err != nil {
        log.Println("Error in encoding gob")
        return "", err
   }
  err = dbSession.DBSession.Put([]byte(user.UserID), network.Bytes(), nil)
  </code></pre>
  
  <p>Since the key for all the entries is the unique uuid, I want to make a secondary index on email so that I dont necessarily have to scan all the entries present in the database to find a particular entry corresponding to an Email.</p>
  
  <p>What I have Done:
  I have created a key called as SIndex and stored a map[string][string] data structure in it, where a key will be an email and value will be the uuid. Every time a new entry comes in, This Sindex will be updated to acommodate  the new uuid and email.</p>
  
  <p>Its a bad approach: 
  Because as data grows, Whole map corresponding to Sindex needs to be fetched and decoded, If email doesn't exists, add a new key to Sindex, encode it and store back again. </p>
  
  <p>A B-tree would be a better fit. </p>
  
  <p>My question : Is it right to store secondary index data in the Database itself, if not what strategies shall I use to implement a secondary Index, I know the choice of secondary index greatly influenced by the data but Are there any good out of box indexing algorithms other than B-Tree, HashMaps?</p>