MCPcopy Create free account
hub / github.com/distributedio/titan / Set

Function Set

command/strings.go:38–115  ·  view source on GitHub ↗

Set key to hold the string value

(ctx *Context, txn *db.Transaction)

Source from the content-addressed store, hash-verified

36
37// Set key to hold the string value
38func Set(ctx *Context, txn *db.Transaction) (OnCommit, error) {
39 key := []byte(ctx.Args[0])
40 value := []byte(ctx.Args[1])
41 args := ctx.Args
42
43 var next bool
44 var flag int // flag int // 0 -- null 1---nx 2---xx
45 var unit int64 // time ms
46 var expire string //expire = expire *unit
47 for i := 2; i < len(args); i++ {
48 if i+1 < len(args) {
49 next = true
50 }
51 if (args[i][0] == 'n' || args[i][0] == 'N') &&
52 (args[i][1] == 'x' || args[i][1] == 'X') &&
53 len(args[i]) == 2 {
54 flag = 1
55 } else if (args[i][0] == 'x' || args[i][0] == 'X') &&
56 (args[i][1] == 'x' || args[i][1] == 'X') &&
57 len(args[i]) == 2 {
58 flag = 2
59 } else if (args[i][0] == 'p' || args[i][0] == 'P') &&
60 (args[i][1] == 'x' || args[i][1] == 'X') &&
61 len(args[i]) == 2 && next {
62 expire = args[i+1]
63 i = i + 1
64 unit = int64(time.Millisecond)
65 } else if (args[i][0] == 'e' || args[i][0] == 'E') &&
66 (args[i][1] == 'x' || args[i][1] == 'X') &&
67 len(args[i]) == 2 && next {
68 expire = args[i+1]
69 i = i + 1
70 unit = int64(time.Second)
71 } else {
72 return nil, ErrSyntax
73 }
74 }
75
76 //ex|px
77 if expire != "" {
78 ui, err := strconv.ParseInt(expire, 10, 64)
79 if err != nil {
80 return nil, ErrInteger
81 }
82 if ui <= 0 {
83 return nil, ErrExpire
84 }
85 unit = ui * unit
86 }
87
88 obj, err := txn.Object(key)
89 if err != nil && err != db.ErrKeyNotFound {
90 return nil, errors.New("ERR " + err.Error())
91 }
92
93 //xx
94 if flag == 2 {
95 if err == db.ErrKeyNotFound {

Callers 2

MSetFunction · 0.70
MSetNxFunction · 0.70

Calls 7

SetMethod · 0.95
NewStringFunction · 0.92
NullBulkStringFunction · 0.85
SimpleStringFunction · 0.85
ObjectMethod · 0.80
ErrorMethod · 0.65
DestoryMethod · 0.65

Tested by

no test coverage detected