yanhongwangg commented on a change in pull request #2046: URL: https://github.com/apache/iotdb/pull/2046#discussion_r528632236
########## File path: client-go/src/main/client/session.go ########## @@ -0,0 +1,698 @@ +/** + * 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" + "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 + +var Log = logrus.New() + +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) + 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).Info("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() error { + 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. Error message") + return err + } else { + Log.WithField("code", status.Code).Info("close session success") + } + s.trans.Close() + return nil +} + +/* + *set one storage group + * + *param + *storageGroupId: string, storage group name (starts from root) + * + *return + *error: correctness of operation + */ +func (s *Session) SetStorageGroup(storageGroupId string) error { + status, err := s.client.SetStorageGroup(context.Background(), s.sessionId, storageGroupId) + if err != nil { + Log.WithError(err).Error("setting storage group failed") + } else { + Log.WithFields(logrus.Fields{ + "sg": storageGroupId, + "code": status.Code, + }).Info("setting storage group success") + } + return err +} + +/* + *delete one storage group + * + *param + *storageGroupId: string, storage group name (starts from root) + * + *return + *error: correctness of operation + */ +func (s *Session) DeleteStorageGroup(storageGroupId string) { Review comment: copy that ---------------------------------------------------------------- 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: [email protected]
