github.com/cyverse/go-irodsclient@v0.13.2/irods/message/open_data_object_request.go (about)

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  
     7  	"github.com/cyverse/go-irodsclient/irods/common"
     8  	"github.com/cyverse/go-irodsclient/irods/types"
     9  	"golang.org/x/xerrors"
    10  )
    11  
    12  // IRODSMessageOpenDataObjectRequest stores data object open request
    13  type IRODSMessageOpenDataObjectRequest IRODSMessageDataObjectRequest
    14  
    15  // NewIRODSMessageOpenDataObjectRequest creates a IRODSMessageOpenDataObjectRequest message
    16  func NewIRODSMessageOpenDataObjectRequest(path string, resource string, mode types.FileOpenMode) *IRODSMessageOpenDataObjectRequest {
    17  	flag := mode.GetFlag()
    18  	request := &IRODSMessageOpenDataObjectRequest{
    19  		Path:          path,
    20  		CreateMode:    0,
    21  		OpenFlags:     flag,
    22  		Offset:        0,
    23  		Size:          -1,
    24  		Threads:       0,
    25  		OperationType: 0,
    26  		KeyVals: IRODSMessageSSKeyVal{
    27  			Length: 0,
    28  		},
    29  	}
    30  
    31  	if len(resource) > 0 {
    32  		request.KeyVals.Add(string(common.DEST_RESC_NAME_KW), resource)
    33  	}
    34  
    35  	return request
    36  }
    37  
    38  // NewIRODSMessageOpenobjRequestWithOperation ...
    39  func NewIRODSMessageOpenobjRequestWithOperation(path string, resource string, mode types.FileOpenMode, oper common.OperationType) *IRODSMessageOpenDataObjectRequest {
    40  	flag := mode.GetFlag()
    41  	request := &IRODSMessageOpenDataObjectRequest{
    42  		Path:          path,
    43  		CreateMode:    0,
    44  		OpenFlags:     flag,
    45  		Offset:        0,
    46  		Size:          -1,
    47  		Threads:       0,
    48  		OperationType: int(oper),
    49  		KeyVals: IRODSMessageSSKeyVal{
    50  			Length: 0,
    51  		},
    52  	}
    53  
    54  	if len(resource) > 0 {
    55  		request.KeyVals.Add(string(common.DEST_RESC_NAME_KW), resource)
    56  	}
    57  
    58  	return request
    59  }
    60  
    61  // NewIRODSMessageOpenobjRequestForPutParallel ...
    62  func NewIRODSMessageOpenobjRequestForPutParallel(path string, resource string, mode types.FileOpenMode, oper common.OperationType, threadNum int, dataSize int64) *IRODSMessageOpenDataObjectRequest {
    63  	flag := mode.GetFlag()
    64  	request := &IRODSMessageOpenDataObjectRequest{
    65  		Path:          path,
    66  		CreateMode:    0,
    67  		OpenFlags:     flag,
    68  		Offset:        0,
    69  		Size:          -1,
    70  		Threads:       0,
    71  		OperationType: int(oper),
    72  		KeyVals: IRODSMessageSSKeyVal{
    73  			Length: 0,
    74  		},
    75  	}
    76  
    77  	if len(resource) > 0 {
    78  		request.KeyVals.Add(string(common.RESC_NAME_KW), resource)
    79  		request.KeyVals.Add(string(common.DEST_RESC_NAME_KW), resource)
    80  	}
    81  
    82  	request.AddKeyVal(common.NUM_THREADS_KW, fmt.Sprintf("%d", threadNum))
    83  	request.AddKeyVal(common.DATA_SIZE_KW, fmt.Sprintf("%d", dataSize))
    84  
    85  	return request
    86  }
    87  
    88  // NewIRODSMessageOpenobjRequestWithReplicaToken creates a IRODSMessageOpenobjRequest message
    89  func NewIRODSMessageOpenobjRequestWithReplicaToken(path string, mode types.FileOpenMode, resourceHierarchy string, replicaToken string, threadNum int, dataSize int64) *IRODSMessageOpenDataObjectRequest {
    90  	flag := mode.GetFlag()
    91  	request := &IRODSMessageOpenDataObjectRequest{
    92  		Path:          path,
    93  		CreateMode:    0,
    94  		OpenFlags:     flag,
    95  		Offset:        0,
    96  		Size:          -1,
    97  		Threads:       0,
    98  		OperationType: 0,
    99  		KeyVals: IRODSMessageSSKeyVal{
   100  			Length: 0,
   101  		},
   102  	}
   103  
   104  	request.AddKeyVal(common.RESC_HIER_STR_KW, resourceHierarchy)
   105  	request.AddKeyVal(common.REPLICA_TOKEN_KW, replicaToken)
   106  	request.AddKeyVal(common.NUM_THREADS_KW, fmt.Sprintf("%d", threadNum))
   107  	request.AddKeyVal(common.DATA_SIZE_KW, fmt.Sprintf("%d", dataSize))
   108  
   109  	return request
   110  }
   111  
   112  // AddKeyVal adds a key-value pair
   113  func (msg *IRODSMessageOpenDataObjectRequest) AddKeyVal(key common.KeyWord, val string) {
   114  	msg.KeyVals.Add(string(key), val)
   115  }
   116  
   117  // GetBytes returns byte array
   118  func (msg *IRODSMessageOpenDataObjectRequest) GetBytes() ([]byte, error) {
   119  	xmlBytes, err := xml.Marshal(msg)
   120  	if err != nil {
   121  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
   122  	}
   123  	return xmlBytes, nil
   124  }
   125  
   126  // FromBytes returns struct from bytes
   127  func (msg *IRODSMessageOpenDataObjectRequest) FromBytes(bytes []byte) error {
   128  	err := xml.Unmarshal(bytes, msg)
   129  	if err != nil {
   130  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
   131  	}
   132  	return nil
   133  }
   134  
   135  // GetMessage builds a message
   136  func (msg *IRODSMessageOpenDataObjectRequest) GetMessage() (*IRODSMessage, error) {
   137  	bytes, err := msg.GetBytes()
   138  	if err != nil {
   139  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
   140  	}
   141  
   142  	msgBody := IRODSMessageBody{
   143  		Type:    RODS_MESSAGE_API_REQ_TYPE,
   144  		Message: bytes,
   145  		Error:   nil,
   146  		Bs:      nil,
   147  		IntInfo: int32(common.DATA_OBJ_OPEN_AN),
   148  	}
   149  
   150  	msgHeader, err := msgBody.BuildHeader()
   151  	if err != nil {
   152  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
   153  	}
   154  
   155  	return &IRODSMessage{
   156  		Header: msgHeader,
   157  		Body:   &msgBody,
   158  	}, nil
   159  }