<commit_before>package db

import (
    "redis"
    "os"
    "fmt"
    "strconv"
    "strings"
    "uniqush"
)

type UniqushRedisDB struct {
    client *redis.Client
}

const (
    DELIVERY_POINT_PREFIX string = "delivery.point:"
    PUSH_SERVICE_PROVIDER_PREFIX string = "push.service.provider:"
)

func NewUniqushRedisDB(c *DatabaseConfig) *UniqushRedisDB {
    if strings.ToLower(c.Engine) != "redis" {
        return nil
    }
    var client redis.Client
    if c.Host == "" {
        c.Host = "localhost"
    }
    if c.Port <= 0 {
        c.Port = 6379
    }
    if c.Name == "" {
        c.Name = "0"
    }

    client.Addr = fmt.Sprintf("%s:%d", c.Host, c.Port)
    client.Password = <PASSWORD>
    var err os.Error
    client.Db, err = strconv.Atoi(c.Name)
    if err != nil {
        client.Db = 0
    }

    ret := new(UniqushRedisDB)
    ret.client = &client
    return ret
}

func keyValueToDeliveryPoint(name string, value []byte) *uniqush.DeliveryPoint {
    v := string(value)
    var substr string
    var ostype int
    fmt.Sscanf(v, "%d.%s", &ostype, &substr)
    switch (ostype) {
    case uniqush.OSTYPE_ANDROID:
        fields := strings.Split(substr, ":")
        if len(fields) < 2 {
            return nil
        }
        ret := uniqush.NewAndroidDeliveryPoint(name, fields[0], fields[1])
        return ret
    }
    return nil
}

func deliveryPointToValue(dp *uniqush.DeliveryPoint) []byte {
    switch (dp.OSID()) {
    case uniqush.OSTYPE_ANDROID:
        str := fmt.Sprintf("%d.%s:%s", dp.OSID(), dp.GoogleAccount(), dp.RegistrationID())
        return []byte(str)
    }
    return nil
}

func (r *UniqushRedisDB) GetDeliveryPoint(name string) (*uniqush.DeliveryPoint, os.Error) {
    b, err := r.client.Get(DELIVERY_POINT_PREFIX + name)
    if err != nil {
        return nil, err
    }
    if b == nil {
        return nil, nil
    }
    return keyValueToDeliveryPoint(name, b), nil
}

func (r *UniqushRedisDB) SetDeliveryPoint(dp *uniqush.DeliveryPoint) os.Error {
    err := r.client.Set(DELIVERY_POINT_PREFIX + dp.Name, deliveryPointToValue(dp))
    return err
}
<commit_msg>tested get/set of RedisDB Benchmarked get/set w/ wt/ cache<commit_after>package db

import (
    "redis"
    "os"
    "fmt"
    "strconv"
    "strings"
    "uniqush"
)

type UniqushRedisDB struct {
    client *redis.Client
}

const (
    DELIVERY_POINT_PREFIX string = "delivery.point:"
    PUSH_SERVICE_PROVIDER_PREFIX string = "push.service.provider:"
)

func NewUniqushRedisDB(c *DatabaseConfig) *UniqushRedisDB {
    if strings.ToLower(c.Engine) != "redis" {
        return nil
    }
    var client redis.Client
    if c.Host == "" {
        c.Host = "localhost"
    }
    if c.Port <= 0 {
        c.Port = 6379
    }
    if c.Name == "" {
        c.Name = "0"
    }

    client.Addr = fmt.Sprintf("%s:%d", c.Host, c.Port)
    client.Password = <PASSWORD>
    var err os.Error
    client.Db, err = strconv.Atoi(c.Name)
    if err != nil {
        client.Db = 0
    }

    ret := new(UniqushRedisDB)
    ret.client = &client
    return ret
}

func keyValueToDeliveryPoint(name string, value []byte) *uniqush.DeliveryPoint {
    v := string(value)
    var substr string
    var ostype int
    fmt.Sscanf(v, "%d.%s", &ostype, &substr)
    switch (ostype) {
    case uniqush.OSTYPE_ANDROID:
        fields := strings.Split(substr, ":")
        if len(fields) < 2 {
            return nil
        }
        return uniqush.NewAndroidDeliveryPoint(name, fields[0], fields[1])
    }
    return nil
}

func keyValueToPushServiceProvider(name string, value []byte) *uniqush.PushServiceProvider {
    v := string(value)
    var substr string
    var srvtype int
    fmt.Sscanf(v, "%d.%s", &srvtype, &substr)
    switch (srvtype) {
    case uniqush.SRVTYPE_C2DM:
        fields := strings.Split(substr, ":")
        if len(fields) < 2 {
            return nil
        }
        return uniqush.NewC2DMServiceProvider(name, fields[0], fields[1])
    }
    return nil
}

func deliveryPointToValue(dp *uniqush.DeliveryPoint) []byte {
    switch (dp.OSID()) {
    case uniqush.OSTYPE_ANDROID:
        str := fmt.Sprintf("%d.%s:%s", dp.OSID(), dp.GoogleAccount(), dp.RegistrationID())
        return []byte(str)
    }
    return nil
}

func pushServiceProviderToValue(psp *uniqush.PushServiceProvider) []byte {
    switch (psp.ServiceID()) {
    case uniqush.SRVTYPE_C2DM:
        str := fmt.Sprintf("%d.%s:%s", psp.ServiceID(), psp.SenderID(), psp.AuthToken())
        return []byte(str)
    }
    return nil
}

func (r *UniqushRedisDB) GetDeliveryPoint(name string) (*uniqush.DeliveryPoint, os.Error) {
    b, err := r.client.Get(DELIVERY_POINT_PREFIX + name)
    if err != nil {
        return nil, err
    }
    if b == nil {
        return nil, nil
    }
    return keyValueToDeliveryPoint(name, b), nil
}

func (r *UniqushRedisDB) SetDeliveryPoint(dp *uniqush.DeliveryPoint) os.Error {
    err := r.client.Set(DELIVERY_POINT_PREFIX + dp.Name, deliveryPointToValue(dp))
    return err
}

func (r *UniqushRedisDB) GetPushServiceProvider(name string) (*uniqush.PushServiceProvider, os.Error) {
    b, err := r.client.Get(PUSH_SERVICE_PROVIDER_PREFIX + name)
    if err != nil {
        return nil, err
    }
    if b == nil {
        return nil, nil
    }
    return keyValueToPushServiceProvider(name, b), nil
}

func (r *UniqushRedisDB) SetPushServiceProvider(psp *uniqush.PushServiceProvider) os.Error {
    return r.client.Set(PUSH_SERVICE_PROVIDER_PREFIX + psp.Name, pushServiceProviderToValue(psp))
}

func (r *UniqushRedisDB) RemoveDeliveryPoint(dp *uniqush.DeliveryPoint) os.Error {
    _, err := r.client.Del(DELIVERY_POINT_PREFIX + dp.Name)
    return err
}

func (r *UniqushRedisDB) RemovePushServiceProvider(psp *uniqush.PushServiceProvider) os.Error {
    _, err := r.client.Del(PUSH_SERVICE_PROVIDER_PREFIX + psp.Name)
    return err
}

{'fraction_non_alphanumeric': 0.127841394633396, 'fraction_numerical': 0.004443684840198256, 'mean_word_length': 2.770618556701031, 'pattern_counts': {'":': 3, '<': 10, '<?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': '6932919', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2088, 'n_words': 441}