<commit_before>package abklevigoNS

import (
  "fmt"
  "strings"

  "github.com/jmhodges/levigo"

  "github.com/abhishekkr/levigoNS/leveldb"
  golhashmap "github.com/abhishekkr/gol/golhashmap"
)

var (
  separator = ":"
)


/*
Reads all direct child values in a given NameSpace
For e.g.:
  given keys a, a:b, a:b:1, a:b:2, a:b:2:3
  reads for a:b:1, a:b:2 if queried for a:b
*/
func ReadNS(key string, db *levigo.DB) golhashmap.HashMap{
  var hmap golhashmap.HashMap
  hmap = make(golhashmap.HashMap)
  key = "key::" + key
  val := abkleveldb.GetVal(key, db)
  if val == "" { return hmap }
  children := strings.Split(val, ",")
  for _, child := range children {
    child_key := "val::" + strings.Split(child, "key::")[1]
    child_val := abkleveldb.GetVal(child_key, db)
    if child_val != "" { hmap[child] = child_val }
  }
  return hmap
}


/*
Reads all values belonging to tree below given NameSpace
For e.g.:
  given keys a, a:b, a:b:1, a:b:2, a:b:2:3
  reads for a:b:1, a:b:2, a:b:2:3 if queried for a:b
*/
func ReadNSRecursive(key string, db *levigo.DB) golhashmap.HashMap{
  var hmap golhashmap.HashMap
  hmap = make(golhashmap.HashMap)

  keyname := "key::" + key
  valname := "val::" + key
  keyname_val := abkleveldb.GetVal(keyname, db)
  valname_val := abkleveldb.GetVal(valname, db)
  if valname_val != "" { hmap[key] = valname_val }
  if keyname_val == "" { return hmap }
  children := strings.Split(keyname_val, ",")

  for _, child_val_as_key := range children {
    child_key := strings.Split(child_val_as_key, "key::")[1]
    inhmap := ReadNSRecursive(child_key, db)
    for inhmap_key, inhmap_val := range inhmap {
      hmap[inhmap_key] = inhmap_val
    }
  }

  return hmap
}


/*
Given all full child keynames of a given NameSpace reside as string separated
by a comma(","). This method checks for a given keyname being a child keyname
for provided for group string of all child keynames.
Return:
  true if given keyname is present as child in group-val of child keynames
  false if not
*/
func IfChildExists(childKey string, parentValue string) bool {
  children := strings.Split(parentValue, ",")
  for _, child := range children {
    if child == childKey {
      return true
    }
  }
  return false
}


/*
Given a parent keyname and child keyname,
updates the group-val for child keynames of a parent keyname as required.
*/
func AppendKey(parent string, child string, db *levigo.DB){
  parentKeyName := fmt.Sprintf("key::%s", parent)
  childKeyName := fmt.Sprintf("key::%s:%s", parent, child)

  val := abkleveldb.GetVal(parentKeyName, db)
  if val == "" {
    abkleveldb.PushKeyVal(parentKeyName, childKeyName, db)
  } else if IfChildExists(childKeyName, val) {
    abkleveldb.PushKeyVal(parentKeyName, val, db)
  } else {
    val = fmt.Sprintf("%s,%s", val, childKeyName)
    abkleveldb.PushKeyVal(parentKeyName, val, db)
  }
}


/*
Given a keyname takes care of updating entry of all trail of NameSpaces.
*/
func CreateNS(key string, db *levigo.DB){
  splitIndex := strings.LastIndexAny(key, separator)
  if splitIndex >= 0 {
    parentKey := key[0:splitIndex]
    childKey := key[splitIndex+1:]

    AppendKey(parentKey, childKey, db)
    CreateNS(parentKey, db)
  }
}


/*
Standard function to feed in NameSpace entries given namespace key and val.
*/
func PushNS(key string, val string, db *levigo.DB) bool{
  CreateNS(key, db)
  key = "val::" + key
  return abkleveldb.PushKeyVal(key, val, db)
}
<commit_msg>[abklevigoNS] AppendKey to private and return status for CreateNS<commit_after>package abklevigoNS

import (
  "fmt"
  "strings"

  "github.com/jmhodges/levigo"

  "github.com/abhishekkr/levigoNS/leveldb"
  golhashmap "github.com/abhishekkr/gol/golhashmap"
)

var (
  separator = ":"
)


/*
Reads all direct child values in a given NameSpace
For e.g.:
  given keys a, a:b, a:b:1, a:b:2, a:b:2:3
  reads for a:b:1, a:b:2 if queried for a:b
*/
func ReadNS(key string, db *levigo.DB) golhashmap.HashMap{
  var hmap golhashmap.HashMap
  hmap = make(golhashmap.HashMap)
  key = "key::" + key
  val := abkleveldb.GetVal(key, db)
  if val == "" { return hmap }
  children := strings.Split(val, ",")
  for _, child := range children {
    child_key := "val::" + strings.Split(child, "key::")[1]
    child_val := abkleveldb.GetVal(child_key, db)
    if child_val != "" { hmap[child] = child_val }
  }
  return hmap
}


/*
Reads all values belonging to tree below given NameSpace
For e.g.:
  given keys a, a:b, a:b:1, a:b:2, a:b:2:3
  reads for a:b:1, a:b:2, a:b:2:3 if queried for a:b
*/
func ReadNSRecursive(key string, db *levigo.DB) golhashmap.HashMap{
  var hmap golhashmap.HashMap
  hmap = make(golhashmap.HashMap)

  keyname := "key::" + key
  valname := "val::" + key
  keyname_val := abkleveldb.GetVal(keyname, db)
  valname_val := abkleveldb.GetVal(valname, db)
  if valname_val != "" { hmap[key] = valname_val }
  if keyname_val == "" { return hmap }
  children := strings.Split(keyname_val, ",")

  for _, child_val_as_key := range children {
    child_key := strings.Split(child_val_as_key, "key::")[1]
    inhmap := ReadNSRecursive(child_key, db)
    for inhmap_key, inhmap_val := range inhmap {
      hmap[inhmap_key] = inhmap_val
    }
  }

  return hmap
}


/*
Given all full child keynames of a given NameSpace reside as string separated
by a comma(","). This method checks for a given keyname being a child keyname
for provided for group string of all child keynames.
Return:
  true if given keyname is present as child in group-val of child keynames
  false if not
*/
func IfChildExists(childKey string, parentValue string) bool {
  children := strings.Split(parentValue, ",")
  for _, child := range children {
    if child == childKey {
      return true
    }
  }
  return false
}


/*
Given a parent keyname and child keyname,
updates the group-val for child keynames of a parent keyname as required.
*/
func appendKey(parent string, child string, db *levigo.DB) bool{
  parentKeyName := fmt.Sprintf("key::%s", parent)
  childKeyName := fmt.Sprintf("key::%s:%s", parent, child)
  status := true

  val := abkleveldb.GetVal(parentKeyName, db)
  if val == "" {
    if ! abkleveldb.PushKeyVal(parentKeyName, childKeyName, db){
      status = false
    }
  } else if IfChildExists(childKeyName, val) {
    if ! abkleveldb.PushKeyVal(parentKeyName, val, db){
      status = false
    }
  } else {
    val = fmt.Sprintf("%s,%s", val, childKeyName)
    if ! abkleveldb.PushKeyVal(parentKeyName, val, db){
      status = false
    }
  }
  return status
}


/*
Given a keyname takes care of updating entry of all trail of NameSpaces.
*/
func CreateNS(key string, db *levigo.DB){
  splitIndex := strings.LastIndexAny(key, separator)
  if splitIndex >= 0 {
    parentKey := key[0:splitIndex]
    childKey := key[splitIndex+1:]

    if appendKey(parentKey, childKey, db){
      CreateNS(parentKey, db)
    }
  }
}


/*
Standard function to feed in NameSpace entries given namespace key and val.
*/
func PushNS(key string, val string, db *levigo.DB) bool{
  CreateNS(key, db)
  key = "val::" + key
  return abkleveldb.PushKeyVal(key, val, db)
}

{'fraction_non_alphanumeric': 0.1277675927231702, 'fraction_numerical': 0.005358905655055705, 'mean_word_length': 3.718562874251497, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3247095', 'n_tokens_mistral': 2628, 'n_tokens_neox': 2600, 'n_words': 715}