sostituito INSERT con UPSERT

aggiunto numero versione
This commit is contained in:
Michele Fadda 2015-07-16 15:52:42 +02:00
parent b311c3596d
commit 654509ff48

View file

@ -24,6 +24,7 @@ type Options struct {
ll *mgo.Collection
RedisTTL time.Duration
LogFile string
Version bool
}
type MongoLogin struct {
@ -33,19 +34,28 @@ type MongoLogin struct {
Date time.Time `json:"date"`
}
type Index struct {
User string `json:"user"`
Date time.Time `json:"date"`
}
const (
_VERSION = "v1.0.2"
)
var (
opts = Options{
MongoUri: "mongodb://127.0.0.1:27018",
RedisUri: "redis-ll.mail.tiscali.sys:6379",
//MongoUri: "mongodb://10.39.81.85:27018",
//RedisUri: "127.0.0.1:6379",
//MongoUri: "mongodb://127.0.0.1:27018",
//RedisUri: "redis-ll.mail.tiscali.sys:6379",
MongoUri: "mongodb://10.39.81.85:27018",
RedisUri: "127.0.0.1:6379",
RedisTTL: time.Hour * 11688, // 16 mesi
LogFile: "log/llmongo.log",
}
)
func usage() {
fmt.Println("Usage: llmongo -m <mongo uri> -r <redis uri> -t <ttl> -l <logfile>\n")
fmt.Println("Usage: llmongo -m <mongo uri> -r <redis uri> -t <ttl> -l <logfile> -v\n")
os.Exit(0)
}
@ -63,6 +73,7 @@ func init() {
flag.StringVar(&opts.RedisUri, "r", opts.RedisUri, "Redis")
flag.StringVar(&opts.LogFile, "l", opts.LogFile, "Logs filename")
flag.DurationVar(&opts.RedisTTL, "t", opts.RedisTTL, "Redis TTL")
flag.BoolVar(&opts.Version, "v", false, "Version")
}
func connectRedis() {
@ -88,6 +99,11 @@ func main() {
flag.Usage = usage
flag.Parse()
if opts.Version {
fmt.Println(os.Args[0], _VERSION)
os.Exit(0)
}
fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Println("Log file error: ", err.Error())
@ -158,8 +174,12 @@ func main() {
Ip: sval[2],
Date: time.Unix(date, 0),
}
ind := Index{
User: user,
Date: time.Unix(date, 0),
}
// inserisce il login su Mongodb
err = opts.ll.Insert(ml)
_, err = opts.ll.Upsert(ind, ml)
if err != nil {
log.Printf("Insert error: %+v\n", err)
// se l'errore non e' "duplicate key error" salta al prossimo senza cancellare niente