Exemple #1
0
        private SDBMessage AdminCommand(string command, BsonDocument matcher, BsonDocument selector,
                                        BsonDocument orderBy, BsonDocument hint)
        {
            BsonDocument dummyObj   = new BsonDocument();
            IConnection  connection = sdb.Connection;
            SDBMessage   sdbMessage = new SDBMessage();

            sdbMessage.OperationCode      = Operation.OP_QUERY;
            sdbMessage.CollectionFullName = command;
            sdbMessage.Version            = SequoiadbConstants.DEFAULT_VERSION;
            sdbMessage.W               = SequoiadbConstants.DEFAULT_W;
            sdbMessage.Padding         = 0;
            sdbMessage.Flags           = 0;
            sdbMessage.NodeID          = SequoiadbConstants.ZERO_NODEID;
            sdbMessage.RequestID       = 0;
            sdbMessage.SkipRowsCount   = 0;
            sdbMessage.ReturnRowsCount = -1;
            // matcher
            if (null == matcher)
            {
                sdbMessage.Matcher = dummyObj;
            }
            else
            {
                sdbMessage.Matcher = matcher;
            }
            // selector
            if (null == selector)
            {
                sdbMessage.Selector = dummyObj;
            }
            else
            {
                sdbMessage.Selector = selector;
            }
            // orderBy
            if (null == orderBy)
            {
                sdbMessage.OrderBy = dummyObj;
            }
            else
            {
                sdbMessage.OrderBy = orderBy;
            }
            // hint
            if (null == hint)
            {
                sdbMessage.Hint = dummyObj;
            }
            else
            {
                sdbMessage.Hint = hint;
            }

            byte[] request = SDBMessageHelper.BuildQueryRequest(sdbMessage, isBigEndian);
            connection.SendMessage(request);
            SDBMessage rtnMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian);

            rtnMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnMessage);
            return(rtnMessage);
        }
Exemple #2
0
        private SDBMessage AdminCommand(string command, BsonDocument arg1, BsonDocument arg2,
                                        BsonDocument arg3, BsonDocument arg4)
        {
            IConnection  connection = group.SequoiaDB.Connection;
            BsonDocument dummyObj   = new BsonDocument();
            SDBMessage   sdbMessage = new SDBMessage();

            sdbMessage.OperationCode = Operation.OP_QUERY;
            sdbMessage.Matcher       = arg1;
            sdbMessage.Selector      = arg2;
            sdbMessage.OrderBy       = arg3;
            sdbMessage.Hint          = arg4;
            // arg1
            if (null == arg1)
            {
                sdbMessage.Matcher = dummyObj;
            }
            else
            {
                sdbMessage.Matcher = arg1;
            }
            // arg2
            if (null == arg2)
            {
                sdbMessage.Selector = dummyObj;
            }
            else
            {
                sdbMessage.Selector = arg2;
            }
            // arg3
            if (null == arg3)
            {
                sdbMessage.OrderBy = dummyObj;
            }
            else
            {
                sdbMessage.OrderBy = arg3;
            }
            // arg4
            if (null == arg4)
            {
                sdbMessage.Hint = dummyObj;
            }
            else
            {
                sdbMessage.Hint = arg4;
            }
            sdbMessage.CollectionFullName = command;
            sdbMessage.Flags           = 0;
            sdbMessage.NodeID          = SequoiadbConstants.ZERO_NODEID;
            sdbMessage.RequestID       = 0;
            sdbMessage.SkipRowsCount   = -1;
            sdbMessage.ReturnRowsCount = -1;

            byte[] request = SDBMessageHelper.BuildQueryRequest(sdbMessage, isBigEndian);
            connection.SendMessage(request);
            SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian);

            rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage);
            return(rtnSDBMessage);
        }
        private void _Open()
        {
            /*
             *  /// open reg msg is |MsgOpLob|bsonobj|
             *  struct _MsgHeader
             *  {
             *     SINT32 messageLength ; // total message size, including this
             *     SINT32 opCode ;        // operation code
             *     UINT32 TID ;           // client thead id
             *     MsgRouteID routeID ;   // route id 8 bytes
             *     UINT64 requestID ;     // identifier for this message
             *  } ;
             *
             *  typedef struct _MsgOpLob
             *  {
             *     MsgHeader header ;
             *     INT32 version ;
             *     SINT16 w ;
             *     SINT16 padding ;
             *     SINT32 flags ;
             *     SINT64 contextID ;
             *     UINT32 bsonLen ;
             *  } MsgOpLob ;
             */
            // add info into object
            BsonDocument openLob = new BsonDocument();

            openLob.Add(SequoiadbConstants.FIELD_COLLECTION, _cl.FullName);
            openLob.Add(SequoiadbConstants.FIELD_LOB_OID, _id);
            openLob.Add(SequoiadbConstants.FIELD_LOB_OPEN_MODE, _mode);

            SDBMessage sdbMessage = new SDBMessage();

            // build sdbMessage
            // MsgHeader
            sdbMessage.OperationCode = Operation.MSG_BS_LOB_OPEN_REQ;
            sdbMessage.NodeID        = SequoiadbConstants.ZERO_NODEID;
            sdbMessage.RequestID     = 0;
            // the rest part of _MsgOpLOb
            sdbMessage.Version       = SequoiadbConstants.DEFAULT_VERSION;
            sdbMessage.W             = SequoiadbConstants.DEFAULT_W;
            sdbMessage.Padding       = (short)0;
            sdbMessage.Flags         = SequoiadbConstants.DEFAULT_FLAGS;
            sdbMessage.ContextIDList = new List <long>();
            sdbMessage.ContextIDList.Add(SequoiadbConstants.DEFAULT_CONTEXTID);
            sdbMessage.Matcher = openLob;
            // build send msg
            byte[] request = SDBMessageHelper.BuildOpenLobRequest(sdbMessage, _isBigEndian);
            // send msg
            _connection.SendMessage(request);
            // receive and extract return msg from engine
            SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(_connection.ReceiveMessage(_isBigEndian), _isBigEndian);
            // check the result
            int flags = rtnSDBMessage.Flags;

            if (flags != 0)
            {
                throw new BaseException(flags);
            }
            // get lob info return from engine
            List <BsonDocument> objList = rtnSDBMessage.ObjectList;
            BsonDocument        obj     = objList[0];

            if (null == obj)
            {
                throw new BaseException("SDB_SYS");
            }
            // lob size
            if (obj.Contains(SequoiadbConstants.FIELD_LOB_SIZE) && obj[SequoiadbConstants.FIELD_LOB_SIZE].IsInt64)
            {
                _size = obj[SequoiadbConstants.FIELD_LOB_SIZE].AsInt64;
            }
            else
            {
                throw new BaseException("SDB_SYS");
            }
            // lob create time
            if (obj.Contains(SequoiadbConstants.FIELD_LOB_CREATTIME) && obj[SequoiadbConstants.FIELD_LOB_CREATTIME].IsInt64)
            {
                _createTime = obj[SequoiadbConstants.FIELD_LOB_CREATTIME].AsInt64;
            }
            else
            {
                throw new BaseException("SDB_SYS");
            }
            // contextID
            _contextID = rtnSDBMessage.ContextIDList[0];
        }
        /** \fn          Write( byte[] b )
         *  \brief       Writes b.length bytes from the specified
         *               byte array to this lob.
         *  \param       b   the data.
         *  \exception SequoiaDB.BaseException
         *  \exception System.Exception
         */
        public void Write(byte[] b)
        {
            /*
             *  /// write req msg is |MsgOpLob|_MsgLobTuple|data|
             *  struct _MsgHeader
             *  {
             *     SINT32 messageLength ; // total message size, including this
             *     SINT32 opCode ;        // operation code
             *     UINT32 TID ;           // client thead id
             *     MsgRouteID routeID ;   // route id 8 bytes
             *     UINT64 requestID ;     // identifier for this message
             *  } ;
             *
             *  typedef struct _MsgOpLob
             *  {
             *     MsgHeader header ;
             *     INT32 version ;
             *     SINT16 w ;
             *     SINT16 padding ;
             *     SINT32 flags ;
             *     SINT64 contextID ;
             *     UINT32 bsonLen ;
             *  } MsgOpLob ;
             *
             *  union _MsgLobTuple
             *  {
             *     struct
             *     {
             *        UINT32 len ;
             *        UINT32 sequence ;
             *        SINT64 offset ;
             *     } columns ;
             *
             *     CHAR data[16] ;
             *  } ;
             */
            SDBMessage sdbMessage = new SDBMessage();

            // MsgHeader
            sdbMessage.OperationCode = Operation.MSG_BS_LOB_WRITE_REQ;
            sdbMessage.NodeID        = SequoiadbConstants.ZERO_NODEID;
            sdbMessage.RequestID     = 0;
            // the rest part of _MsgOpLOb
            sdbMessage.Version       = SequoiadbConstants.DEFAULT_VERSION;
            sdbMessage.W             = SequoiadbConstants.DEFAULT_W;
            sdbMessage.Padding       = (short)0;
            sdbMessage.Flags         = SequoiadbConstants.DEFAULT_FLAGS;
            sdbMessage.ContextIDList = new List <long>();
            sdbMessage.ContextIDList.Add(_contextID);
            sdbMessage.BsonLen = 0;
            // MsgLobTuple
            sdbMessage.LobLen      = (uint)b.Length;
            sdbMessage.LobSequence = SDB_LOB_DEFAULT_SEQ;
            sdbMessage.LobOffset   = SDB_LOB_DEFAULT_OFFSET;

            // build send msg
            byte[] request = SDBMessageHelper.BuildWriteLobRequest(sdbMessage, b, _isBigEndian);
            // send msg
            _connection.SendMessage(request);

            // receive and extract return msg from engine
            SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(_connection.ReceiveMessage(_isBigEndian), _isBigEndian);

            // check the result
            int flags = rtnSDBMessage.Flags;

            if (0 != flags)
            {
                throw new BaseException(flags);
            }
            // keep to total number for query
            _size += b.Length;
        }
Exemple #5
0
        private ByteBuffer _GenerateWriteLobRequest(byte[] input, int off, int len)
        {
            /*
             *  /// write req msg is |MsgOpLob|_MsgLobTuple|data|
             *  struct _MsgHeader
             *  {
             *     SINT32 messageLength ; // total message size, including this
             *     SINT32 opCode ;        // operation code
             *     UINT32 TID ;           // client thead id
             *     MsgRouteID routeID ;   // route id 8 bytes
             *     UINT64 requestID ;     // identifier for this message
             *  } ;
             *
             *  typedef struct _MsgOpLob
             *  {
             *     MsgHeader header ;
             *     INT32 version ;
             *     SINT16 w ;
             *     SINT16 padding ;
             *     SINT32 flags ;
             *     SINT64 contextID ;
             *     UINT32 bsonLen ;
             *  } MsgOpLob ;
             *
             *  union _MsgLobTuple
             *  {
             *     struct
             *     {
             *        UINT32 len ;
             *        UINT32 sequence ;
             *        SINT64 offset ;
             *     } columns ;
             *
             *     CHAR data[16] ;
             *  } ;
             */
            if (off + len > input.Length)
            {
                throw new BaseException((int)Errors.errors.SDB_SYS, "off + len is more than input.length");
            }
            int totalLen = SDBMessageHelper.MESSAGE_OPLOB_LENGTH
                           + SDBMessageHelper.MESSAGE_LOBTUPLE_LENGTH
                           + Helper.RoundToMultipleXLength(len, 4);
            // alloc ByteBuffer
            ByteBuffer totalBuf = new ByteBuffer(totalLen);

            totalBuf.IsBigEndian = _isBigEndian;

            // MsgHeader
            SDBMessageHelper.AddMsgHeader(totalBuf, totalLen,
                                          (int)Operation.MSG_BS_LOB_WRITE_REQ,
                                          SequoiadbConstants.ZERO_NODEID, 0);

            // MsgOpLob
            SDBMessageHelper.AddLobOpMsg(totalBuf, SequoiadbConstants.DEFAULT_VERSION,
                                         SequoiadbConstants.DEFAULT_W, (short)0,
                                         SequoiadbConstants.DEFAULT_FLAGS, _contextID, 0);

            // MsgLobTuple
            AddMsgTuple(totalBuf, len, SDB_LOB_DEFAULT_SEQ,
                        SDB_LOB_DEFAULT_OFFSET);

            // lob data
            SDBMessageHelper.AddBytesToByteBuffer(totalBuf, input, off, len, 4);

            return(totalBuf);
        }
Exemple #6
0
        private int _OnceRead(byte[] buf, int off, int len)
        {
            int needRead   = len;
            int totalRead  = 0;
            int onceRead   = 0;
            int alignedLen = 0;

            // try to get data from local needRead
            if (_HasDataCached())
            {
                onceRead     = _ReadInCache(buf, off, needRead);
                totalRead   += onceRead;
                needRead    -= onceRead;
                _readOffset += onceRead;
                return(totalRead);
            }

            // get data from database
            _cachedOffset   = -1;
            _cachedDataBuff = null;

            // page align
            alignedLen = _ReviseReadLen(needRead);
            // build read message
            ByteBuffer request = _GenerateReadLobRequest(alignedLen);
            // seed and receive message to engine
            ByteBuffer respone = _SendAndReiveMessage(request);
            // extract return message
            SDBMessage retInfo = SDBMessageHelper.ExtractLobReadReply(respone);

            if (retInfo.OperationCode != Operation.MSG_BS_LOB_READ_RES)
            {
                throw new BaseException((int)Errors.errors.SDB_UNKNOWN_MESSAGE,
                                        string.Format("Receive Unexpected operation code: {0}", retInfo.OperationCode));
            }
            int rc = retInfo.Flags;

            // meet the end of the lob
            if (rc == (int)Errors.errors.SDB_EOF)
            {
                return(-1);
            }
            if (rc != 0)
            {
                throw new BaseException(rc);
            }
            // sanity check
            // return message is |MsgOpReply|_MsgLobTuple|data|
            int retMsgLen = retInfo.RequestLength;

            if (retMsgLen < SDBMessageHelper.MESSAGE_OPREPLY_LENGTH +
                SDBMessageHelper.MESSAGE_LOBTUPLE_LENGTH)
            {
                throw new BaseException((int)Errors.errors.SDB_SYS,
                                        "invalid message's length: " + retMsgLen);
            }
            long offsetInEngine = retInfo.LobOffset;

            if (_readOffset != offsetInEngine)
            {
                throw new BaseException((int)Errors.errors.SDB_SYS,
                                        "local read offset(" + _readOffset +
                                        ") is not equal with what we expect(" + offsetInEngine + ")");
            }
            int retLobLen = (int)retInfo.LobLen;

            if (retMsgLen < SDBMessageHelper.MESSAGE_OPREPLY_LENGTH +
                SDBMessageHelper.MESSAGE_LOBTUPLE_LENGTH + retLobLen)
            {
                throw new BaseException((int)Errors.errors.SDB_SYS,
                                        "invalid message's length: " + retMsgLen);
            }
            /// get return data
            _cachedDataBuff          = retInfo.LobCachedDataBuf;
            retInfo.LobCachedDataBuf = null;
            // sanity check
            int remainLen = _cachedDataBuff.Remaining();

            if (remainLen != retLobLen)
            {
                throw new BaseException((int)Errors.errors.SDB_SYS, "the remaining in buffer(" + remainLen +
                                        ") is not equal with what we expect(" + retLobLen + ")");
            }
            // if what we got is more than what we expect,
            // let's cache some for next reading request.
            int copy   = (needRead < retLobLen) ? needRead : retLobLen;
            int output = _cachedDataBuff.PopByteArray(buf, off, copy);

            if (copy != output)
            {
                throw new BaseException((int)Errors.errors.SDB_SYS,
                                        string.Format("Invalid bytes length in the cached buffer, copy {0} bytes, actually output {1} bytes", copy, output));
            }
            totalRead   += output;
            _readOffset += output;
            if (needRead < retLobLen)
            {
                _cachedOffset = _readOffset;
            }
            else
            {
                _cachedOffset   = -1;
                _cachedDataBuff = null;
            }
            return(totalRead);
        }
Exemple #7
0
        /************************************** private methond **************************************/

        private void _Open()
        {
            /*
             *  /// open reg msg is |MsgOpLob|bsonobj|
             *  struct _MsgHeader
             *  {
             *     SINT32 messageLength ; // total message size, including this
             *     SINT32 opCode ;        // operation code
             *     UINT32 TID ;           // client thead id
             *     MsgRouteID routeID ;   // route id 8 bytes
             *     UINT64 requestID ;     // identifier for this message
             *  } ;
             *
             *  typedef struct _MsgOpLob
             *  {
             *     MsgHeader header ;
             *     INT32 version ;
             *     SINT16 w ;
             *     SINT16 padding ;
             *     SINT32 flags ;
             *     SINT64 contextID ;
             *     UINT32 bsonLen ;
             *  } MsgOpLob ;
             */
            // add info into object
            BsonDocument openLob = new BsonDocument();

            openLob.Add(SequoiadbConstants.FIELD_COLLECTION, _cl.FullName);
            openLob.Add(SequoiadbConstants.FIELD_LOB_OID, _id);
            openLob.Add(SequoiadbConstants.FIELD_LOB_OPEN_MODE, _mode);

            // set return data flag
            int flags = (_mode == SDB_LOB_READ) ? FLG_LOBOPEN_WITH_RETURNDATA :
                        SequoiadbConstants.DEFAULT_FLAGS;

            // generate request
            ByteBuffer request = _GenerateOpenLobRequest(openLob, flags);

            // send and receive message
            ByteBuffer respone = _SendAndReiveMessage(request);

            // extract info from the respone
            SDBMessage retInfo = SDBMessageHelper.ExtractLobOpenReply(respone);

            // check the respone opcode
            if (retInfo.OperationCode != Operation.MSG_BS_LOB_OPEN_RES)
            {
                throw new BaseException((int)Errors.errors.SDB_UNKNOWN_MESSAGE,
                                        string.Format("Receive Unexpected operation code: {0}", retInfo.OperationCode));
            }

            // check the result
            int rc = retInfo.Flags;

            if (rc != 0)
            {
                throw new BaseException(rc);
            }
            // get lob info return from engine
            List <BsonDocument> objList = retInfo.ObjectList;

            if (objList.Count() != 1)
            {
                throw new BaseException((int)Errors.errors.SDB_NET_BROKEN_MSG);
            }
            BsonDocument obj = objList[0];

            if (null == obj)
            {
                throw new BaseException((int)Errors.errors.SDB_SYS);
            }
            // lob size
            if (obj.Contains(SequoiadbConstants.FIELD_LOB_SIZE) && obj[SequoiadbConstants.FIELD_LOB_SIZE].IsInt64)
            {
                _size = obj[SequoiadbConstants.FIELD_LOB_SIZE].AsInt64;
            }
            else
            {
                throw new BaseException((int)Errors.errors.SDB_SYS);
            }
            // lob create time
            if (obj.Contains(SequoiadbConstants.FIELD_LOB_CREATTIME) && obj[SequoiadbConstants.FIELD_LOB_CREATTIME].IsInt64)
            {
                _createTime = obj[SequoiadbConstants.FIELD_LOB_CREATTIME].AsInt64;
            }
            else
            {
                throw new BaseException((int)Errors.errors.SDB_SYS);
            }
            // page size
            if (obj.Contains(SequoiadbConstants.FIELD_LOB_PAGESIZE) && obj[SequoiadbConstants.FIELD_LOB_PAGESIZE].IsInt32)
            {
                _pageSize = obj[SequoiadbConstants.FIELD_LOB_PAGESIZE].AsInt32;
            }
            else
            {
                throw new BaseException((int)Errors.errors.SDB_SYS);
            }
            // cache data
            _cachedDataBuff = retInfo.LobCachedDataBuf;
            if (_cachedDataBuff != null)
            {
                _readOffset   = 0;
                _cachedOffset = _readOffset;
            }
            retInfo.LobCachedDataBuf = null;
            // contextID
            _contextID = retInfo.ContextIDList[0];
        }