github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/collection_attr.go (about) 1 // Copyright (C) 2019-2021 Zilliz. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance 4 // with the License. You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software distributed under the License 9 // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 10 // or implied. See the License for the specific language governing permissions and limitations under the License. 11 12 package entity 13 14 import ( 15 "strconv" 16 "strings" 17 18 "github.com/cockroachdb/errors" 19 ) 20 21 const ( 22 // cakTTL const for collection attribute key TTL in seconds. 23 cakTTL = `collection.ttl.seconds` 24 // cakAutoCompaction const for collection attribute key autom compaction enabled. 25 cakAutoCompaction = `collection.autocompaction.enabled` 26 akMmap = "mmap.enabled" 27 databaseReplica = "database.replica" 28 databaseResourceGroups = "database.resource_groups" 29 ) 30 31 // CollectionAttribute is the interface for altering collection attributes. 32 type CollectionAttribute interface { 33 KeyValue() (string, string) 34 Valid() error 35 } 36 37 type collAttrBase struct { 38 key string 39 value string 40 } 41 42 // KeyValue implements CollectionAttribute. 43 func (ca collAttrBase) KeyValue() (string, string) { 44 return ca.key, ca.value 45 } 46 47 type ttlCollAttr struct { 48 collAttrBase 49 } 50 51 // Valid implements CollectionAttribute. 52 // checks ttl seconds is valid positive integer. 53 func (ca collAttrBase) Valid() error { 54 val, err := strconv.ParseInt(ca.value, 10, 64) 55 if err != nil { 56 return errors.Wrap(err, "ttl is not a valid positive integer") 57 } 58 59 if val < 0 { 60 return errors.New("ttl needs to be a positive integer") 61 } 62 63 return nil 64 } 65 66 // CollectionTTL returns collection attribute to set collection ttl in seconds. 67 func CollectionTTL(ttl int64) ttlCollAttr { 68 ca := ttlCollAttr{} 69 ca.key = cakTTL 70 ca.value = strconv.FormatInt(ttl, 10) 71 return ca 72 } 73 74 type autoCompactionCollAttr struct { 75 collAttrBase 76 } 77 78 // Valid implements CollectionAttribute. 79 // checks collection auto compaction is valid bool. 80 func (ca autoCompactionCollAttr) Valid() error { 81 _, err := strconv.ParseBool(ca.value) 82 if err != nil { 83 return errors.Wrap(err, "auto compaction setting is not valid boolean") 84 } 85 86 return nil 87 } 88 89 // CollectionAutoCompactionEnabled returns collection attribute to set collection auto compaction enabled. 90 func CollectionAutoCompactionEnabled(enabled bool) autoCompactionCollAttr { 91 ca := autoCompactionCollAttr{} 92 ca.key = cakAutoCompaction 93 ca.value = strconv.FormatBool(enabled) 94 return ca 95 } 96 97 type mmapAttr struct { 98 collAttrBase 99 } 100 101 func Mmap(enabled bool) mmapAttr { 102 attr := mmapAttr{} 103 attr.key = akMmap 104 attr.value = strconv.FormatBool(enabled) 105 return attr 106 } 107 108 func (ca mmapAttr) Valid() error { 109 _, err := strconv.ParseBool(ca.value) 110 if err != nil { 111 return errors.Wrap(err, "mmap setting is not valid boolean") 112 } 113 114 return nil 115 } 116 117 type DatabaseAttribute = CollectionAttribute 118 119 type databaseAttrBase = collAttrBase 120 121 type databaseReplicaAttr struct { 122 databaseAttrBase 123 } 124 125 func DatabaseReplica(replica int64) databaseReplicaAttr { 126 attr := databaseReplicaAttr{} 127 attr.key = databaseReplica 128 attr.value = strconv.FormatInt(replica, 10) 129 return attr 130 } 131 132 func (ca databaseReplicaAttr) Valid() error { 133 value, err := strconv.ParseInt(ca.value, 10, 64) 134 if err != nil || value <= 0 { 135 return errors.Wrap(err, "replica setting is not valid integer") 136 } 137 138 return nil 139 } 140 141 type databaseResourceGroupsAttr struct { 142 collAttrBase 143 } 144 145 func DatabaseResourceGroups(resourceGroups []string) databaseResourceGroupsAttr { 146 attr := databaseResourceGroupsAttr{} 147 attr.key = databaseResourceGroups 148 attr.value = strconv.Quote(strings.Join(resourceGroups, ",")) 149 return attr 150 } 151 152 func (ca databaseResourceGroupsAttr) Valid() error { 153 values := strings.Split(ca.value, ",") 154 if len(values) == 0 { 155 return errors.New("resource groups setting is not valid") 156 } 157 158 return nil 159 }