yanhongwangg commented on a change in pull request #2046:
URL: https://github.com/apache/iotdb/pull/2046#discussion_r530095804



##########
File path: client-go/src/main/client/session.go
##########
@@ -0,0 +1,667 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package client
+
+import (
+       "bytes"
+       "context"
+       "encoding/binary"
+       "fmt"
+       "github.com/apache/thrift/lib/go/thrift"
+       log "github.com/sirupsen/logrus"
+       "github.com/yanhongwangg/go-thrift/rpc"
+       
"github.com/yanhongwangg/incubator-iotdb/client-go/src/main/client/utils"
+       "net"
+       "os"
+       "strings"
+       "time"
+)
+
+const protocolVersion = rpc.TSProtocolVersion_IOTDB_SERVICE_PROTOCOL_V3
+
+func (s *Session) Open(enableRPCCompression bool, connectionTimeoutInMs int) {
+       dir, _ := os.Getwd()
+       os.Mkdir(dir+"\\logs", os.ModePerm)
+       logFile, _ := os.OpenFile(dir+"\\logs\\all.log", 
os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
+       log.SetOutput(logFile)
+       log.SetLevel(log.InfoLevel)
+       var protocolFactory thrift.TProtocolFactory
+       s.trans, s.err = thrift.NewTSocketTimeout(net.JoinHostPort(s.Host, 
s.Port), time.Duration(connectionTimeoutInMs))
+       if s.err != nil {
+               log.WithError(s.err).Error("connect failed")
+       }
+       s.trans = thrift.NewTFramedTransport(s.trans)
+       if !s.trans.IsOpen() {
+               s.err = s.trans.Open()
+               if s.err != nil {
+                       log.WithError(s.err).Error("open the conn failed")
+               }
+       }
+       if enableRPCCompression {
+               protocolFactory = thrift.NewTCompactProtocolFactory()
+       } else {
+               protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
+       }
+       iProtocol := protocolFactory.GetProtocol(s.trans)
+       oProtocol := protocolFactory.GetProtocol(s.trans)
+       s.client = rpc.NewTSIServiceClient(thrift.NewTStandardClient(iProtocol, 
oProtocol))
+       s.ZoneId = DefaultZoneId
+       tSOpenSessionReq := rpc.TSOpenSessionReq{ClientProtocol: 
protocolVersion, ZoneId: s.ZoneId, Username: &s.User,
+               Password: &s.Passwd}
+       tSOpenSessionResp, err := s.client.OpenSession(context.Background(), 
&tSOpenSessionReq)
+       if err != nil {
+               log.WithError(err).Error("open session failed")
+       } else {
+               log.WithField("code", 
tSOpenSessionResp.GetStatus().Code).Debug("open session success")
+       }
+       s.sessionId = tSOpenSessionResp.GetSessionId()
+       s.requestStatementId, err = 
s.client.RequestStatementId(context.Background(), s.sessionId)
+       s.FetchSize = DefaultFetchSize
+       if err != nil {
+               log.WithError(err).Error("request StatementId failed")
+       }
+       if s.ZoneId != "" {
+               s.SetTimeZone(s.ZoneId)
+       } else {
+               s.ZoneId = s.GetTimeZone()
+       }
+}
+
+func (s *Session) CheckTimeseriesExists(path string) bool {
+       dataSet := s.ExecuteQueryStatement("SHOW TIMESERIES " + path)
+       result := dataSet.HasNext()
+       dataSet.CloseOperationHandle()
+       return result
+}
+
+func (s *Session) Close() {
+       tSCloseSessionReq := rpc.NewTSCloseSessionReq()
+       tSCloseSessionReq.SessionId = s.sessionId
+       status, err := s.client.CloseSession(context.Background(), 
tSCloseSessionReq)
+       if err != nil {
+               log.WithError(err).Error("Error occurs when closing session at 
server. Maybe server is down.")
+       } else {
+               log.WithField("code", status.Code).Debug("close session 
success")
+       }
+       s.trans.Close()
+}
+
+/*
+ *set one storage group
+ *
+ *param
+ *storageGroupId: string, storage group name (starts from root)
+ *
+ */
+func (s *Session) SetStorageGroup(storageGroupId string) {
+       status, err := s.client.SetStorageGroup(context.Background(), 
s.sessionId, storageGroupId)
+       if err != nil {
+               log.WithError(err).Error("setting storage group failed")
+       } else {
+               log.WithFields(log.Fields{
+                       "sg":   storageGroupId,
+                       "code": status.Code,
+               }).Debug("setting storage group success")
+       }
+}
+
+/*
+ *delete one storage group
+ *
+ *param
+ *storageGroupId: string, storage group name (starts from root)
+ *
+ */
+func (s *Session) DeleteStorageGroup(storageGroupId string) {
+       status, err := s.client.DeleteStorageGroups(context.Background(), 
s.sessionId, []string{storageGroupId})
+       if err != nil {
+               log.WithError(err).Error("delete storage group failed")
+       } else {
+               log.WithFields(log.Fields{
+                       "sg":   storageGroupId,
+                       "code": status.Code,
+               }).Debug("delete storage group success")
+       }
+}
+
+/*
+ *delete multiple storage group
+ *
+ *param
+ *storageGroupIds: []string, paths of the target storage groups
+ *
+ */
+func (s *Session) DeleteStorageGroups(storageGroupIds []string) {
+       status, err := s.client.DeleteStorageGroups(context.Background(), 
s.sessionId, storageGroupIds)
+       s.sg = strings.Replace(strings.Trim(fmt.Sprint(storageGroupIds), "[]"), 
" ", ",", -1)

Review comment:
       thanks for your guidance




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to