/** \fn DBCursor Aggregate(List<BsonDocument> obj) * \brief Execute aggregate operation in specified collection * \param insertor The array of bson objects, can't be null * \return The DBCursor of result or null * \exception SequoiaDB.BaseException * \exception System.Exception */ public DBCursor Aggregate(List<BsonDocument> obj) { if ( obj == null || obj.Count == 0 ) throw new BaseException("SDB_INVALIDARG"); SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_AGGREGATE; sdbMessage.Version = SequoiadbConstants.DEFAULT_VERSION; sdbMessage.W = SequoiadbConstants.DEFAULT_W; sdbMessage.Padding = 0; sdbMessage.CollectionFullName = collectionFullName; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; sdbMessage.RequestID = 0; sdbMessage.Flags = 0; sdbMessage.Insertor = obj[0]; byte[] request = SDBMessageHelper.BuildAggrRequest(sdbMessage, isBigEndian); for (int count = 1; count < obj.Count; count++) { request = SDBMessageHelper.AppendAggrMsg(request, obj[count], isBigEndian); } connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags != 0) if (flags == SequoiadbConstants.SDB_DMS_EOC) return null; else { throw new BaseException(flags); } return new DBCursor(rtnSDBMessage, this); }
private SDBMessage AdminCommand(string cmdType, string contextType, string contextName) { IConnection connection = sdb.Connection; BsonDocument dummyObj = new BsonDocument(); SDBMessage sdbMessage = new SDBMessage(); string commandString = SequoiadbConstants.ADMIN_PROMPT + cmdType + " " + contextType; BsonDocument cObj = new BsonDocument(); cObj.Add(SequoiadbConstants.FIELD_NAME, contextName); sdbMessage.OperationCode = Operation.OP_QUERY; sdbMessage.Matcher = cObj; sdbMessage.CollectionFullName = commandString; sdbMessage.Flags = 0; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; sdbMessage.RequestID = 0; sdbMessage.SkipRowsCount = -1; sdbMessage.ReturnRowsCount = -1; sdbMessage.Selector = dummyObj; sdbMessage.OrderBy = dummyObj; sdbMessage.Hint = dummyObj; byte[] request = SDBMessageHelper.BuildQueryRequest(sdbMessage, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); return(rtnSDBMessage); }
/** \fn void DetachNode( string hostName, * int port, * BsonDocument options ) * \brief Detach a node from the group * \param [in] pHostName The host name of node. * \param [in] port The port for the node. * \param [in] optoins The options of detach. * \retval SDB_OK Operation Success * \retval Others Operation Fail */ public void DetachNode(string hostName, int port, BsonDocument options) { if (hostName == null || port < 0 || port > 65535) { throw new BaseException("SDB_INVALIDARG"); } string command = SequoiadbConstants.ADMIN_PROMPT + SequoiadbConstants.REMOVE_CMD + " " + SequoiadbConstants.NODE; BsonDocument newObj = new BsonDocument(); newObj.Add(SequoiadbConstants.FIELD_GROUPNAME, groupName); newObj.Add(SequoiadbConstants.FIELD_HOSTNAME, hostName); newObj.Add(SequoiadbConstants.SVCNAME, port.ToString()); newObj.Add(SequoiadbConstants.FIELD_NAME_ONLY_DETACH, true); if (options != null && options.ElementCount != 0) { foreach (string key in options.Names) { if (SequoiadbConstants.FIELD_NAME_ONLY_DETACH != key) { newObj.Add(options.GetElement(key)); } } } BsonDocument dummyObj = new BsonDocument(); SDBMessage rtn = AdminCommand(command, newObj, dummyObj, dummyObj, dummyObj); int flags = rtn.Flags; if (flags != 0) { throw new BaseException(flags); } }
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.CollectionFullName = command; sdbMessage.Version = 0; sdbMessage.W = 0; 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 rtn = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); return(rtn); }
private SDBMessage AdminCommand(string command, BsonDocument arg1, BsonDocument arg2, BsonDocument arg3, BsonDocument arg4) { IConnection connection = sdb.Connection; BsonDocument dummyObj = new BsonDocument(); SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_QUERY; // 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); }
internal static DBCursor RunGeneralCmd(Sequoiadb sdb, string command, BsonDocument matcher, BsonDocument selector, BsonDocument orderBy, BsonDocument hint, int flags, ulong reqID, long skipNum, long retNum) { IConnection connection = sdb.Connection; bool isBigEndian = sdb.isBigEndian; BsonDocument dummyObj = new BsonDocument(); 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 = flags; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; sdbMessage.RequestID = reqID; sdbMessage.SkipRowsCount = skipNum; sdbMessage.ReturnRowsCount = retNum; sdbMessage.Matcher = (null == matcher) ? dummyObj : matcher; sdbMessage.Selector = (null == selector) ? dummyObj : selector; sdbMessage.OrderBy = (null == orderBy) ? dummyObj : orderBy; sdbMessage.Hint = (null == hint) ? dummyObj : hint; // check if (connection == null) { throw new BaseException("SDB_NETWORK"); } // build msg byte[] request = SDBMessageHelper.BuildQueryRequest(sdbMessage, isBigEndian); // send connection.SendMessage(request); // extract SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); // check return msg header rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); // check whether error happen int errorCode = rtnSDBMessage.Flags; if (errorCode != 0) { // TODO: need to throw error detail(return from engine) out throw new BaseException(errorCode); } // try to build return cursor if (SequoiadbConstants.DEFAULT_CONTEXTID == rtnSDBMessage.ContextIDList[0] && (rtnSDBMessage.ObjectList == null || rtnSDBMessage.ObjectList.Count == 0)) { return(null); } else { return(new DBCursor(rtnSDBMessage, sdb)); } }
/** \fn Close() * \brief Close the lob * \return void * \exception SequoiaDB.BaseException * \exception System.Exception */ public void Close() { /* * /// close reg msg is |MsgOpLob| * 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 ; */ SDBMessage sdbMessage = new SDBMessage(); // build sdbMessage // MsgHeader sdbMessage.OperationCode = Operation.MSG_BS_LOB_CLOSE_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; // build send msg byte[] request = SDBMessageHelper.BuildCloseLobRequest(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); } _isOpen = false; }
/** \fn void DropCollection(string collectionName) * \brief Remove the named collection of current collection space * \param collectionName The collection name * \exception SequoiaDB.BaseException * \exception System.Exception */ public void DropCollection(string collectionName) { SDBMessage rtn = AdminCommand(SequoiadbConstants.DROP_CMD, SequoiadbConstants.COLLECTION, name + "." + collectionName); int flags = rtn.Flags; if (flags != 0) { throw new BaseException(flags); } }
/** \fn DBCollection CreateCollection(string collectionName) * \brief Create the named collection in current collection space * \param collectionName The collection name * \return The DBCollection handle * \exception SequoiaDB.BaseException * \exception System.Exception */ public DBCollection CreateCollection(string collectionName) { SDBMessage rtn = AdminCommand(SequoiadbConstants.CREATE_CMD, SequoiadbConstants.COLLECTION, name + "." + collectionName); int flags = rtn.Flags; if (flags != 0) { throw new BaseException(flags); } return(new DBCollection(this, collectionName.Trim())); }
/** \fn void DropCollection(string collectionName) * \brief Remove the named collection of current collection space * \param collectionName The collection name * \exception SequoiaDB.BaseException * \exception System.Exception */ public void DropCollection(string collectionName) { string fullName = this.Name + "." + collectionName; SDBMessage rtn = AdminCommand(SequoiadbConstants.DROP_CMD, SequoiadbConstants.COLLECTION, fullName); int flags = rtn.Flags; if (flags != 0) { throw new BaseException(flags); } sdb.RemoveCache(fullName); }
private int _Read(byte[] b) { SDBMessage sdbMessage = new SDBMessage(); // MsgHeader sdbMessage.OperationCode = Operation.MSG_BS_LOB_READ_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 = _readOffset; // build send msg byte[] request = SDBMessageHelper.BuildReadLobRequest(sdbMessage, _isBigEndian); // send msg _connection.SendMessage(request); // receive and extract return msg from engine SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReadLobReply(_connection.ReceiveMessage(_isBigEndian), _isBigEndian); // check the result int flags = rtnSDBMessage.Flags; int errCode = new BaseException("SDB_DMS_EOC").ErrorCode; if (errCode == flags) { return(-1); } else if (0 != flags) { throw new BaseException(flags); } // return the result byte[] lobData = rtnSDBMessage.LobBuff; Array.Copy(lobData, b, lobData.Length); _readOffset += lobData.Length; return(lobData.Length); }
internal DBCursor(SDBMessage rtnSDBMessage, Sequoiadb sdb) { this.connection = sdb.Connection; hint = new BsonDocument(); hint.Add("", SequoiadbConstants.CLIENT_RECORD_ID_INDEX); sdbMessage = new SDBMessage(); reqId = rtnSDBMessage.RequestID; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; sdbMessage.ContextIDList = rtnSDBMessage.ContextIDList; contextId = sdbMessage.ContextIDList[0]; sdbMessage.NumReturned = -1; // return data count list = rtnSDBMessage.ObjectList; // while using fineOne, ObjectList may have data hasMore = true; isBigEndian = sdb.isBigEndian; isClosed = false; }
internal DBCursor(SDBMessage rtnSDBMessage, Sequoiadb sdb ) { this.connection = sdb.Connection; hint = new BsonDocument(); hint.Add("", SequoiadbConstants.CLIENT_RECORD_ID_INDEX); sdbMessage = new SDBMessage(); reqId = rtnSDBMessage.RequestID; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; sdbMessage.ContextIDList = rtnSDBMessage.ContextIDList; contextId = sdbMessage.ContextIDList[0]; sdbMessage.NumReturned = -1; // return data count list = rtnSDBMessage.ObjectList; // while using fineOne, ObjectList may have data hasMore = true; isBigEndian = sdb.isBigEndian; isClosed = false; }
/** \fn Close() * \brief Close the lob * \return void * \exception SequoiaDB.BaseException * \exception System.Exception */ public void Close() { if (!_isOpened) { return; } ByteBuffer request = _GenerateCloseLobRequest(); ByteBuffer respone = _SendAndReiveMessage(request); SDBMessage retInfo = SDBMessageHelper.ExtractReply(respone); if (retInfo.OperationCode != Operation.MSG_BS_LOB_CLOSE_RES) { throw new BaseException((int)Errors.errors.SDB_UNKNOWN_MESSAGE, string.Format("Receive Unexpected operation code: {0}", retInfo.OperationCode)); } int errorCode = retInfo.Flags; if (0 != errorCode) { throw new BaseException(errorCode); } // update the last update time List <BsonDocument> resultSet = retInfo.ObjectList; if (resultSet != null && resultSet.Count > 0) { BsonDocument obj = resultSet[0]; if (obj != null && obj.Contains(SequoiadbConstants.FIELD_LOB_MODIFICATION_TIME)) { if (obj[SequoiadbConstants.FIELD_LOB_MODIFICATION_TIME].IsInt64) { _modificationTime = obj[SequoiadbConstants.FIELD_LOB_MODIFICATION_TIME].AsInt64; } else { throw new BaseException((int)Errors.errors.SDB_SYS, "the received data is not a long type."); } } } _isOpened = false; }
/* \fn void UpdateCurrent(BsonDocument modifier) * \brief Update the current Bson of this cursor * \param modifier The updating rule * \exception SequoiaDB.BaseException * \exception System.Exception */ /* * public void UpdateCurrent(BsonDocument modifier) * { * if (modifier == null) * throw new BaseException("SDB_INVALIDARG"); * if (dbc == null) * throw new BaseException("SDB_CLT_OBJ_NOT_EXIST"); * BsonDocument current; * if (( current = Current()) != null ) * { * BsonDocument matcher = new BsonDocument(); * matcher.Add(SequoiadbConstants.OID, current[SequoiadbConstants.OID].AsObjectId); * dbc.Update(matcher, modifier, hint); * BsonDocument dummy = new BsonDocument(); * list[index] = dbc.Query(matcher, dummy, dummy, dummy).Next(); * } * } */ /* \fn void DeleteCurrent() * \brief Delete the current Bson of this cursor * \exception SequoiaDB.BaseException * \exception System.Exception */ /* * public void DeleteCurrent() * { * if ( Current() != null ) * dbc.Delete( list[index] ); * list.RemoveAt(index); * if (index >= list.Count) * index = -1; * } */ private void ReadNextBuffer() { if (connection == null) { throw new BaseException("SDB_NOT_CONNECTED"); } if (-1 == contextId) { hasMore = false; index = -1; dbc = null; list = null; return; } sdbMessage.OperationCode = Operation.OP_GETMORE; sdbMessage.RequestID = reqId; byte[] request = SDBMessageHelper.BuildGetMoreRequest(sdbMessage, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags == SequoiadbConstants.SDB_DMS_EOC) { hasMore = false; index = -1; dbc = null; list = null; } else if (flags != 0) { throw new BaseException(flags); } else { reqId = rtnSDBMessage.RequestID; list = rtnSDBMessage.ObjectList; } }
/** \fn Node CreateNode(string hostName, int port, string dbpath, * Dictionary<string, string> map) * \brief Create the replica node * \param hostName The host name of node * \param port The port of node * \param dbpath The database path of node * \param map The other configure information of node * \return The Node object * \exception SequoiaDB.BaseException * \exception System.Exception */ public Node CreateNode(string hostName, int port, string dbpath, Dictionary <string, string> map) { if (hostName == null || port < 0 || port < 0 || port > 65535 || dbpath == null) { throw new BaseException("SDB_INVALIDARG"); } string command = SequoiadbConstants.ADMIN_PROMPT + SequoiadbConstants.CREATE_CMD + " " + SequoiadbConstants.NODE; BsonDocument configuration = new BsonDocument(); configuration.Add(SequoiadbConstants.FIELD_GROUPNAME, groupName); map.Remove(SequoiadbConstants.FIELD_GROUPNAME); configuration.Add(SequoiadbConstants.FIELD_HOSTNAME, hostName); map.Remove(SequoiadbConstants.FIELD_HOSTNAME); configuration.Add(SequoiadbConstants.SVCNAME, port.ToString()); map.Remove(SequoiadbConstants.SVCNAME); configuration.Add(SequoiadbConstants.DBPATH, dbpath); map.Remove(SequoiadbConstants.DBPATH); Dictionary <string, string> .Enumerator it = map.GetEnumerator(); while (it.MoveNext()) { configuration.Add(it.Current.Key, it.Current.Value); } BsonDocument dummyObj = new BsonDocument(); SDBMessage rtn = AdminCommand(command, configuration, dummyObj, dummyObj, dummyObj); int flags = rtn.Flags; if (flags != 0) { throw new BaseException(flags); } else { return(GetNode(hostName, port)); } }
private bool StopStart(bool start) { string command = start ? SequoiadbConstants.ADMIN_PROMPT + SequoiadbConstants.STARTUP_CMD + " " + SequoiadbConstants.NODE : SequoiadbConstants.ADMIN_PROMPT + SequoiadbConstants.SHUTDOWN_CMD + " " + SequoiadbConstants.NODE; BsonDocument configuration = new BsonDocument(); configuration.Add(SequoiadbConstants.FIELD_HOSTNAME, hostName); configuration.Add(SequoiadbConstants.SVCNAME, port.ToString()); BsonDocument dummyObj = new BsonDocument(); SDBMessage rtn = AdminCommand(command, configuration, dummyObj, dummyObj, dummyObj); int flags = rtn.Flags; if (flags != 0) { return(false); } else { return(true); } }
private bool StopStart(bool start) { string command = start ? SequoiadbConstants.ADMIN_PROMPT + SequoiadbConstants.ACTIVE_CMD + " " + SequoiadbConstants.GROUP : SequoiadbConstants.ADMIN_PROMPT + SequoiadbConstants.SHUTDOWN_CMD + " " + SequoiadbConstants.GROUP; BsonDocument configuration = new BsonDocument(); configuration.Add(SequoiadbConstants.FIELD_GROUPNAME, groupName); configuration.Add(SequoiadbConstants.FIELD_GROUPID, groupID); BsonDocument dummyObj = new BsonDocument(); SDBMessage rtn = AdminCommand(command, configuration, dummyObj, dummyObj, dummyObj); int flags = rtn.Flags; if (flags != 0) { return(false); } else { return(true); } }
/** \fn SDBConst.NodeStatus GetStatus() * \brief Get the status of current node * \return The status of current node * \exception SequoiaDB.BaseException * \exception System.Exception * \deprecated Since v2.8, the status of node are invalid, nerver use this api again. */ public SDBConst.NodeStatus GetStatus() { SDBConst.NodeStatus status = SDBConst.NodeStatus.SDB_NODE_UNKNOWN; string command = SequoiadbConstants.ADMIN_PROMPT + SequoiadbConstants.SNAP_CMD + " " + SequoiadbConstants.DATABASE; BsonDocument condition = new BsonDocument(); BsonDocument dummyObj = new BsonDocument(); condition.Add(SequoiadbConstants.FIELD_GROUPID, group.GroupID); condition.Add(SequoiadbConstants.FIELD_NODEID, nodeID); SDBMessage rtn = AdminCommand(command, condition, dummyObj, dummyObj, dummyObj); int flags = rtn.Flags; if (flags == 0) { status = SDBConst.NodeStatus.SDB_NODE_ACTIVE; } else if (flags == (int)Errors.errors.SDB_NET_CANNOT_CONNECT) { status = SDBConst.NodeStatus.SDB_NODE_INACTIVE; } return(status); }
/** \fn bool IsCollectionExist(string colName) * \brief Verify the existence of collection in current colleciont space * \param colName The collection name * \return True if collection existed or False if not existed * \exception SequoiaDB.BaseException * \exception System.Exception */ public bool IsCollectionExist(string colName) { string command = SequoiadbConstants.ADMIN_PROMPT + SequoiadbConstants.TEST_CMD + " " + SequoiadbConstants.COLLECTION; BsonDocument condition = new BsonDocument(); BsonDocument dummyObj = new BsonDocument(); condition.Add(SequoiadbConstants.FIELD_NAME, this.name + "." + colName); SDBMessage rtn = AdminCommand(command, condition, dummyObj, dummyObj, dummyObj); int flags = rtn.Flags; if (flags == 0) { return(true); } else if (flags == (int)Errors.errors.SDB_DMS_NOTEXIST) { return(false); } else { throw new BaseException(flags); } }
/** \fn void RemoveNode(string hostName, int port, * BsonDocument configure) * \brief Remove the specified replica node * \param hostName The host name of node * \param port The port of node * \param configure The configurations for the replica node * \exception SequoiaDB.BaseException * \exception System.Exception */ public void RemoveNode(string hostName, int port, BsonDocument configure) { if (hostName == null || port < 0 || port < 0 || port > 65535) { throw new BaseException("SDB_INVALIDARG"); } string command = SequoiadbConstants.ADMIN_PROMPT + SequoiadbConstants.REMOVE_CMD + " " + SequoiadbConstants.NODE; BsonDocument config = new BsonDocument(); config.Add(SequoiadbConstants.FIELD_GROUPNAME, groupName); config.Add(SequoiadbConstants.FIELD_HOSTNAME, hostName); config.Add(SequoiadbConstants.SVCNAME, Convert.ToString(port)); if (configure != null) { foreach (string key in configure.Names) { if (key.Equals(SequoiadbConstants.FIELD_GROUPNAME) || key.Equals(SequoiadbConstants.FIELD_HOSTNAME) || key.Equals(SequoiadbConstants.SVCNAME)) { continue; } config.Add(configure.GetElement(key)); } } BsonDocument dummyObj = new BsonDocument(); SDBMessage rtn = AdminCommand(command, config, dummyObj, dummyObj, dummyObj); int flags = rtn.Flags; if (flags != 0) { throw new BaseException(flags); } }
private void KillCursor() { if ((connection == null) || (connection.IsClosed()) || (contextId == -1)) { return; } long[] contextIds = new long[1] { contextId }; byte[] request = SDBMessageHelper.BuildKillCursorMsg(contextIds, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); int flags = rtnSDBMessage.Flags; if (flags != 0) { throw new BaseException(flags); } connection = null; contextId = -1; }
internal static byte[] BuildKillCursorMsg(SDBMessage sdbMessage, bool isBigEndian) { int opCode = (int)sdbMessage.OperationCode; List<long> contextIDs = sdbMessage.ContextIDList; ulong requestID = 0; byte[] nodeID = SequoiadbConstants.ZERO_NODEID; int lenContextIDs = sizeof(long) * contextIDs.Count; int messageLength = MESSAGE_KILLCURSOR_LENGTH + lenContextIDs; List<byte[]> fieldList = new List<byte[]>(); fieldList.Add(AssembleHeader(messageLength, requestID, nodeID, opCode, isBigEndian)); ByteBuffer buf = new ByteBuffer(8 + lenContextIDs); if (isBigEndian) buf.IsBigEndian = true; int zero = 0; int numContexts = 1; buf.PushInt(zero); buf.PushInt(numContexts); foreach (long contextID in contextIDs) buf.PushLong(contextID); fieldList.Add(buf.ToByteArray()); byte[] msgInByteArray = Helper.ConcatByteArray(fieldList); if (logger.IsDebugEnabled) { StringWriter buff = new StringWriter(); foreach (byte by in msgInByteArray) { buff.Write(string.Format("{0:X}", by)); } logger.Debug("Disconnect Request string==>" + buff.ToString() + "<=="); } return msgInByteArray; }
/** \fn ObjectId Insert(BsonDocument insertor) * \brief Insert a document into current collection * \param insertor The Bson document of insertor, can't be null * \return ObjectId * \exception SequoiaDB.BaseException * \exception System.Exception */ public BsonValue Insert(BsonDocument insertor) { if (insertor == null) throw new BaseException("SDB_INVALIDARG"); SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_INSERT; sdbMessage.Version = SequoiadbConstants.DEFAULT_VERSION; sdbMessage.W = SequoiadbConstants.DEFAULT_W; sdbMessage.Padding = 0; sdbMessage.CollectionFullName = collectionFullName; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; sdbMessage.RequestID = 0; sdbMessage.Insertor = insertor; ObjectId objId; BsonValue tmp; //if (insertor. if (insertor.TryGetValue(SequoiadbConstants.OID, out tmp)) { ; } else { objId = ObjectId.GenerateNewId(); tmp = objId; insertor.Add(SequoiadbConstants.OID, objId); } byte[] request = SDBMessageHelper.BuildInsertRequest(sdbMessage, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags != 0) throw new BaseException(flags); return tmp; }
/** \fn void BulkInsert(List<BsonDocument> insertor, int flag) * \brief Insert a bulk of bson objects into current collection * \param insertor The Bson document of insertor list, can't be null * \param flag FLG_INSERT_CONTONDUP or 0 * \exception SequoiaDB.BaseException * \exception System.Exception */ public void BulkInsert(List<BsonDocument> insertor, int flag) { if ( insertor == null || insertor.Count == 0 ) throw new BaseException("SDB_INVALIDARG"); SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_INSERT; sdbMessage.Version = SequoiadbConstants.DEFAULT_VERSION; sdbMessage.W = SequoiadbConstants.DEFAULT_W; sdbMessage.Padding = 0; sdbMessage.CollectionFullName = collectionFullName; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; sdbMessage.RequestID = 0; if (flag != 0 && flag != SDBConst.FLG_INSERT_CONTONDUP) throw new BaseException(flag); sdbMessage.Flags = flag; sdbMessage.Insertor = insertor[0]; byte[] request = SDBMessageHelper.BuildInsertRequest(sdbMessage, isBigEndian); for (int count = 1; count < insertor.Count; count++) { request = SDBMessageHelper.AppendInsertMsg(request, insertor[count], isBigEndian); } connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags != 0) throw new BaseException(flags); }
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]; }
/************************************** 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]; }
/** \fn Close() * \brief Close the lob * \return void * \exception SequoiaDB.BaseException * \exception System.Exception */ public void Close() { /* /// close reg msg is |MsgOpLob| 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 ; */ SDBMessage sdbMessage = new SDBMessage(); // build sdbMessage // MsgHeader sdbMessage.OperationCode = Operation.MSG_BS_LOB_CLOSE_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; // build send msg byte[] request = SDBMessageHelper.BuildCloseLobRequest(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); } _isOpen = false; }
private SDBMessage AdminCommand(string command, BsonDocument arg1, BsonDocument arg2, BsonDocument arg3, BsonDocument arg4) { IConnection connection = sdb.Connection; BsonDocument dummyObj = new BsonDocument(); SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_QUERY; // 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 int _Read(byte[] b) { SDBMessage sdbMessage = new SDBMessage(); // MsgHeader sdbMessage.OperationCode = Operation.MSG_BS_LOB_READ_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 = _readOffset; // build send msg byte[] request = SDBMessageHelper.BuildReadLobRequest(sdbMessage, _isBigEndian); // send msg _connection.SendMessage(request); // receive and extract return msg from engine SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReadLobReply(_connection.ReceiveMessage(_isBigEndian), _isBigEndian); // check the result int flags = rtnSDBMessage.Flags; int errCode = new BaseException("SDB_DMS_EOC").ErrorCode; if ( errCode == flags) { return -1; } else if (0 != flags) { throw new BaseException(flags); } // return the result byte[] lobData = rtnSDBMessage.LobBuff; Array.Copy(lobData, b, lobData.Length); _readOffset += lobData.Length; return lobData.Length; }
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; }
internal static byte[] BuildGetMoreRequest(SDBMessage sdbMessage, bool isBigEndian) { int opCode = (int)sdbMessage.OperationCode; ulong requestID = sdbMessage.RequestID; long contextId = sdbMessage.ContextIDList[0]; int numReturned = sdbMessage.NumReturned; byte[] nodeID = sdbMessage.NodeID; int messageLength = MESSAGE_OPGETMORE_LENGTH; List<byte[]> fieldList = new List<byte[]>(); fieldList.Add(AssembleHeader(messageLength, requestID, nodeID, opCode, isBigEndian)); ByteBuffer buf = new ByteBuffer(12); if (isBigEndian) buf.IsBigEndian = true; buf.PushLong(contextId); buf.PushInt(numReturned); fieldList.Add(buf.ToByteArray()); byte[] msgInByteArray = Helper.ConcatByteArray(fieldList); if (logger.IsDebugEnabled) { StringWriter buff = new StringWriter(); foreach (byte by in msgInByteArray) { buff.Write(string.Format("{0:X}", by)); } logger.Debug("GetMore Request string==>" + buff.ToString() + "<=="); } return msgInByteArray; }
internal static byte[] BuildTransactionRequest(SDBMessage sdbMessage, bool isBigEndian) { int opcode = (int)sdbMessage.OperationCode; ulong requestID = sdbMessage.RequestID; byte[] nodeID = SequoiadbConstants.ZERO_NODEID; int messageLength = Helper.RoundToMultipleXLength(MESSAGE_HEADER_LENGTH, 4); byte[] msgInByteArray = AssembleHeader(messageLength, requestID, nodeID, (int)opcode, isBigEndian); if (logger.IsDebugEnabled) { StringWriter buff = new StringWriter(); foreach (byte by in msgInByteArray) { buff.Write(string.Format("{0:X}", by)); } logger.Debug("Disconnect Request string==>" + buff.ToString() + "<=="); } return msgInByteArray; }
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); }
/** \fn bool IsValid() * \brief Judge wether the connection is is valid or not * \return If the connection is valid, return true */ public bool IsValid() { if (connection == null || connection.IsClosed()) { return false; } int flags = -1; // send a lightweight query msg to engine to check // wether connection is ok or not try { SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_KILL_CONTEXT; sdbMessage.ContextIDList = new List<long>(); sdbMessage.ContextIDList.Add(-1); byte[] request = SDBMessageHelper.BuildKillCursorMsg(sdbMessage, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); flags = rtnSDBMessage.Flags; if (flags != 0) { throw new BaseException(flags); } } catch (System.Exception) { return false; } return true; }
internal static byte[] BuildRemoveLobRequest(SDBMessage sdbMessage, bool isBigEndian) { /* /// remove lob 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 ; */ // get info to build _MsgOpLob // MsgHeader int messageLength = 0; int opCode = (int)sdbMessage.OperationCode; byte[] nodeID = sdbMessage.NodeID; ulong requestID = sdbMessage.RequestID; // the rest part of _MsgOpLOb int version = sdbMessage.Version; short w = sdbMessage.W; short padding = sdbMessage.Padding; int flags = sdbMessage.Flags; long contextID = sdbMessage.ContextIDList[0]; uint bsonLen = 0; byte[] bLob = sdbMessage.Matcher.ToBson(); bsonLen = (uint)bLob.Length; if (isBigEndian) { BsonEndianConvert(bLob, 0, bLob.Length, true); } // calculate total length messageLength = MESSAGE_OPLOB_LENGTH + Helper.RoundToMultipleXLength(bLob.Length, 4); // build a array list for return List<byte[]> fieldList = new List<byte[]>(); // add MsgHead fieldList.Add(AssembleHeader(messageLength, requestID, nodeID, opCode, isBigEndian)); // add the rest part of MsgOpLob ByteBuffer buf = new ByteBuffer(MESSAGE_OPLOB_LENGTH - MESSAGE_HEADER_LENGTH); if (isBigEndian) { buf.IsBigEndian = true; } buf.PushInt(version); buf.PushShort(w); buf.PushShort(padding); buf.PushInt(flags); buf.PushLong(contextID); buf.PushInt((int)bsonLen); // add msg header fieldList.Add(buf.ToByteArray()); // add msg body fieldList.Add(Helper.RoundToMultipleX(bLob, 4)); // convert to byte array and return byte[] msgInByteArray = Helper.ConcatByteArray(fieldList); if (logger.IsDebugEnabled) { StringWriter buff = new StringWriter(); foreach (byte by in msgInByteArray) { buff.Write(string.Format("{0:X}", by)); } logger.Debug("Remove Lob Request string==>" + buff.ToString() + "<=="); } return msgInByteArray; }
/** \fn void TransactionRollback() * \brief Rollback the database transaction * \return void * \exception SequoiaDB.BaseException * \exception System.Exception */ public void TransactionRollback() { SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_TRANS_ROLLBACK; sdbMessage.RequestID = 0; byte[] request = SDBMessageHelper.BuildTransactionRequest(sdbMessage, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags != 0) throw new BaseException(flags); }
internal static byte[] BuildReadLobRequest(SDBMessage sdbMessage, bool isBigEndian) { /* /// read req msg is |MsgOpLob|_MsgLobTuple| 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] ; } ; */ // get info to build _MsgOpLob // MsgHeader int messageLength = 0; int opCode = (int)sdbMessage.OperationCode; byte[] nodeID = sdbMessage.NodeID; ulong requestID = sdbMessage.RequestID; // the rest part of _MsgOpLOb int version = sdbMessage.Version; short w = sdbMessage.W; short padding = sdbMessage.Padding; int flags = sdbMessage.Flags; long contextID = sdbMessage.ContextIDList[0]; uint bsonLen = sdbMessage.BsonLen; // MsgLobTuple uint length = sdbMessage.LobLen; uint sequence = sdbMessage.LobSequence; long offset = sdbMessage.LobOffset; // calculate total length messageLength = MESSAGE_OPLOB_LENGTH + MESSAGE_LOBTUPLE_LENGTH; // build a array list for return List<byte[]> fieldList = new List<byte[]>(); // add MsgHead fieldList.Add(AssembleHeader(messageLength, requestID, nodeID, opCode, isBigEndian)); // add the rest part of MsgOpLob and MsgLobTuple ByteBuffer buf = new ByteBuffer(MESSAGE_OPLOB_LENGTH - MESSAGE_HEADER_LENGTH + MESSAGE_LOBTUPLE_LENGTH); if (isBigEndian) { buf.IsBigEndian = true; } buf.PushInt(version); buf.PushShort(w); buf.PushShort(padding); buf.PushInt(flags); buf.PushLong(contextID); buf.PushInt((int)bsonLen); buf.PushInt((int)length); buf.PushInt((int)sequence); buf.PushLong(offset); fieldList.Add(buf.ToByteArray()); // convert to byte array and return byte[] msgInByteArray = Helper.ConcatByteArray(fieldList); if (logger.IsDebugEnabled) { StringWriter buff = new StringWriter(); foreach (byte by in msgInByteArray) { buff.Write(string.Format("{0:X}", by)); } logger.Debug("Read Lob Request string==>" + buff.ToString() + "<=="); } return msgInByteArray; }
private SDBMessage CreateCS(string csName, BsonDocument options) { string commandString = SequoiadbConstants.ADMIN_PROMPT + SequoiadbConstants.CREATE_CMD + " " + SequoiadbConstants.COLSPACE; BsonDocument cObj = new BsonDocument(); BsonDocument dummyObj = new BsonDocument(); SDBMessage sdbMessage = new SDBMessage(); cObj.Add(SequoiadbConstants.FIELD_NAME, csName); cObj.Add(options); sdbMessage.OperationCode = Operation.OP_QUERY; sdbMessage.Matcher = cObj; sdbMessage.CollectionFullName = commandString; 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; sdbMessage.Selector = dummyObj; sdbMessage.OrderBy = dummyObj; sdbMessage.Hint = dummyObj; byte[] request = SDBMessageHelper.BuildQueryRequest(sdbMessage, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); return rtnSDBMessage; }
/** \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; }
internal static byte[] BuildSqlMsg(SDBMessage sdbMessage, string sql, bool isBigEndian) { int opCode = (int)sdbMessage.OperationCode; ulong requestID = sdbMessage.RequestID; byte[] nodeID = sdbMessage.NodeID; byte[] sqlBytes = System.Text.Encoding.UTF8.GetBytes(sql); int sqlLen = sqlBytes.Length+1; int messageLength = Helper.RoundToMultipleXLength( MESSAGE_HEADER_LENGTH + sqlLen, 4); List<byte[]> fieldList = new List<byte[]>(); fieldList.Add(AssembleHeader(messageLength, requestID, nodeID, opCode, isBigEndian)); byte[] newArray = new byte[sqlLen]; for (int i = 0; i < sqlLen - 1; i++) newArray[i] = sqlBytes[i]; fieldList.Add(Helper.RoundToMultipleX(newArray, 4)); byte[] msgInByteArray = Helper.ConcatByteArray(fieldList); if (logger.IsDebugEnabled) { StringWriter buff = new StringWriter(); foreach (byte by in msgInByteArray) { buff.Write(string.Format("{0:X}", by)); } logger.Debug("SQL Message string==>" + buff.ToString() + "<=="); } return msgInByteArray; }
private List<BsonDocument> GetMoreCommand(SDBMessage rtnSDBMessage) { ulong requestID = rtnSDBMessage.RequestID; List<long> contextIDs = rtnSDBMessage.ContextIDList; List<BsonDocument> fullList = new List<BsonDocument>(); bool hasMore = true; while (hasMore) { SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_GETMORE; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; sdbMessage.ContextIDList = contextIDs; sdbMessage.RequestID = requestID; sdbMessage.NumReturned = -1; byte[] request = SDBMessageHelper.BuildGetMoreRequest(sdbMessage, isBigEndian); connection.SendMessage(request); rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags != 0) if (flags == SequoiadbConstants.SDB_DMS_EOC) hasMore = false; else { throw new BaseException(flags); } else { requestID = rtnSDBMessage.RequestID; List<BsonDocument> objList = rtnSDBMessage.ObjectList; fullList.AddRange(objList); } } return fullList; }
private SDBMessage AdminCommand(string cmdType, string contextType, string contextName) { IConnection connection = sdb.Connection; BsonDocument dummyObj = new BsonDocument(); SDBMessage sdbMessage = new SDBMessage(); string commandString = SequoiadbConstants.ADMIN_PROMPT + cmdType + " " + contextType; BsonDocument cObj = new BsonDocument(); cObj.Add(SequoiadbConstants.FIELD_NAME, contextName); sdbMessage.OperationCode = Operation.OP_QUERY; sdbMessage.Matcher = cObj; sdbMessage.CollectionFullName = commandString; sdbMessage.Flags = 0; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; sdbMessage.RequestID = 0; sdbMessage.SkipRowsCount = -1; sdbMessage.ReturnRowsCount = -1; sdbMessage.Selector = dummyObj; sdbMessage.OrderBy = dummyObj; sdbMessage.Hint = dummyObj; byte[] request = SDBMessageHelper.BuildQueryRequest(sdbMessage, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); return rtnSDBMessage; }
/** \fn void Delete(BsonDocument matcher, BsonDocument hint) * \brief Delete the matching document of current collection * \param matcher The matching condition * \param hint Hint * \exception SequoiaDB.BaseException * \exception System.Exception */ public void Delete(BsonDocument matcher, BsonDocument hint) { SDBMessage sdbMessage = new SDBMessage(); BsonDocument dummyObj = new BsonDocument(); if (matcher == null) matcher = dummyObj; if (hint == null) hint = dummyObj; sdbMessage.OperationCode = Operation.OP_DELETE; sdbMessage.Version = SequoiadbConstants.DEFAULT_VERSION; sdbMessage.W = SequoiadbConstants.DEFAULT_W; sdbMessage.Padding = 0; sdbMessage.Flags = 0; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; sdbMessage.CollectionFullName = collectionFullName; sdbMessage.RequestID = 0; sdbMessage.Matcher = matcher; sdbMessage.Hint = hint; byte[] request = SDBMessageHelper.BuildDeleteRequest(sdbMessage, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags != 0) { throw new BaseException(flags); } }
internal static byte[] BuildKillAllContextsRequest(SDBMessage sdbMessage, bool isBigEndian) { return BuildTransactionRequest(sdbMessage, isBigEndian); }
/** \fn DBLob RemoveLob(ObjectId id) * \brief Remove an existing lob with the speceifed oid * \param id The oid of the existing lob * \exception SequoiaDB.BaseException * \exception System.Exception */ public void RemoveLob(ObjectId id) { BsonDocument newObj = new BsonDocument(); newObj.Add(SequoiadbConstants.FIELD_COLLECTION, collectionFullName); newObj.Add(SequoiadbConstants.FIELD_LOB_OID, id); SDBMessage sdbMessage = new SDBMessage(); // MsgHeader sdbMessage.OperationCode = Operation.MSG_BS_LOB_REMOVE_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 = newObj; byte[] request = SDBMessageHelper.BuildRemoveLobRequest(sdbMessage, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags != 0) { throw new BaseException(flags); } }
/** \fn void ExecUpdate(string sql) * \brief Executing SQL command for updating * \param sql SQL command * \exception SequoiaDB.BaseException * \exception System.Exception */ public void ExecUpdate(string sql) { SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_SQL; sdbMessage.RequestID = 0; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; byte[] request = SDBMessageHelper.BuildSqlMsg(sdbMessage, sql, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags != 0) throw new BaseException(flags); }
/** \fn void CloseAllCursors() * \brief Close all the cursors created in current connection, * we can't use those cursors to get data from db engine again, * but, there are some data cache in local * \return void * \exception SequoiaDB.BaseException * \exception System.Exception */ public void CloseAllCursors() { // TODO: it's better for us to use DBCursor::Close() to close all the cursor SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_KILL_ALL_CONTEXTS; byte[] request = SDBMessageHelper.BuildKillAllContextsRequest(sdbMessage, isBigEndian); connection.SendMessage(request); }
internal static byte[] BuildUpdateRequest(SDBMessage sdbMessage, bool isBigEndian) { int opCode = (int)sdbMessage.OperationCode; string collectionName = sdbMessage.CollectionFullName; int version = sdbMessage.Version; short w = sdbMessage.W; short padding = sdbMessage.Padding; int flags = sdbMessage.Flags; ulong requestID = sdbMessage.RequestID; byte[] nodeID = sdbMessage.NodeID; byte[] collByteArray = System.Text.Encoding.UTF8.GetBytes(collectionName); int collectionNameLength = collByteArray.Length; byte[] matcher = sdbMessage.Matcher.ToBson(); byte[] hint = sdbMessage.Hint.ToBson(); byte[] modifier = sdbMessage.Modifier.ToBson(); if (isBigEndian) { BsonEndianConvert(matcher, 0, matcher.Length, true); BsonEndianConvert(modifier, 0, modifier.Length, true); BsonEndianConvert(hint, 0, hint.Length, true); } int messageLength = Helper.RoundToMultipleXLength( MESSAGE_OPUPDATE_LENGTH + collectionNameLength, 4) + Helper.RoundToMultipleXLength(matcher.Length, 4) + Helper.RoundToMultipleXLength(hint.Length, 4) + Helper.RoundToMultipleXLength(modifier.Length, 4); List<byte[]> fieldList = new List<byte[]>(); fieldList.Add(AssembleHeader(messageLength, requestID, nodeID, opCode, isBigEndian)); ByteBuffer buf = new ByteBuffer(16); if (isBigEndian) buf.IsBigEndian = true; buf.PushInt(version); buf.PushShort(w); buf.PushShort(padding); buf.PushInt(flags); buf.PushInt(collectionNameLength); fieldList.Add(buf.ToByteArray()); byte[] newCollectionName = new byte[collectionNameLength + 1]; for (int i = 0; i < collectionNameLength; i++) newCollectionName[i] = collByteArray[i]; fieldList.Add(Helper.RoundToMultipleX(newCollectionName, 4)); fieldList.Add(Helper.RoundToMultipleX(matcher, 4)); fieldList.Add(Helper.RoundToMultipleX(modifier, 4)); fieldList.Add(Helper.RoundToMultipleX(hint, 4)); byte[] msgInByteArray = Helper.ConcatByteArray(fieldList); if (logger.IsDebugEnabled) { StringWriter buff = new StringWriter(); foreach (byte by in msgInByteArray) { buff.Write(string.Format("{0:X}", by)); } logger.Debug("Update Request string==>" + buff.ToString() + "<=="); } return msgInByteArray; }
/** \fn void CreateUser(string username, string password) * \brief Add an user in current database * \username Sequoiadb connection user name * \password Sequoiadb connection password * \return void * \exception SequoiaDB.BaseException * \exception System.Exception */ public void CreateUser(string username, string password) { if (username == null || password == null) throw new BaseException("SDB_INVALIDARG"); SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.MSG_AUTH_CRTUSR_REQ; sdbMessage.RequestID = 0 ; MD5 md5 = MD5.Create(); byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(password)); StringBuilder builder = new StringBuilder(); for (int i = 0; i < data.Length; i++) builder.Append(data[i].ToString("x2")); byte[] request = SDBMessageHelper.BuildAuthCrtMsg(sdbMessage, username, builder.ToString(), isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags != 0) throw new BaseException(flags); }
/** \fn DBCursor Exec(string sql) * \brief Executing SQL command * \param sql SQL command * \return The DBCursor of matching documents or null * \exception SequoiaDB.BaseException * \exception System.Exception */ public DBCursor Exec(string sql) { SDBMessage sdbMessage = new SDBMessage(); sdbMessage.OperationCode = Operation.OP_SQL; sdbMessage.RequestID = 0; sdbMessage.NodeID = SequoiadbConstants.ZERO_NODEID; byte[] request = SDBMessageHelper.BuildSqlMsg(sdbMessage, sql, isBigEndian); connection.SendMessage(request); SDBMessage rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); int flags = rtnSDBMessage.Flags; if (flags != 0) { if (flags == SequoiadbConstants.SDB_DMS_EOC) return null; else { throw new BaseException(flags); } } if ( null == rtnSDBMessage.ContextIDList || rtnSDBMessage.ContextIDList.Count != 1 || rtnSDBMessage.ContextIDList[0] == -1 ) return null ; return new DBCursor(rtnSDBMessage, this); }
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 rtnSDBMessage = SDBMessageHelper.MsgExtractReply(connection.ReceiveMessage(isBigEndian), isBigEndian); rtnSDBMessage = SDBMessageHelper.CheckRetMsgHeader(sdbMessage, rtnSDBMessage); return rtnSDBMessage; }
internal static byte[] BuildInsertRequest(SDBMessage sdbMessage, bool isBigEndian) { int opCode = (int)sdbMessage.OperationCode; string collectionName = sdbMessage.CollectionFullName; int version = sdbMessage.Version; short w = sdbMessage.W; short padding = sdbMessage.Padding; int flags = sdbMessage.Flags; ulong requestID = sdbMessage.RequestID; byte[] collByteArray = System.Text.Encoding.UTF8.GetBytes(collectionName); int collectionNameLength = collByteArray.Length; byte[] insertor = sdbMessage.Insertor.ToBson(); byte[] nodeID = sdbMessage.NodeID; if (isBigEndian) { BsonEndianConvert(insertor, 0, insertor.Length, true); } // calculate the total length of the packet which to send int messageLength = Helper.RoundToMultipleXLength( MESSAGE_OPINSERT_LENGTH + collectionNameLength, 4) + Helper.RoundToMultipleXLength(insertor.Length, 4); // put all the part of packet into a list, and then transform the list into byte[] // we need byte[] while sending List<byte[]> fieldList = new List<byte[]>(); // let's put the packet head into list fieldList.Add(AssembleHeader(messageLength, requestID, nodeID, opCode, isBigEndian)); ByteBuffer buf = new ByteBuffer(16); if (isBigEndian) buf.IsBigEndian = true; buf.PushInt(version); buf.PushShort(w); buf.PushShort(padding); buf.PushInt(flags); buf.PushInt(collectionNameLength); fieldList.Add(buf.ToByteArray()); // cl name also in the packet head, we need one more byte for '\0' byte[] newCollectionName = new byte[collectionNameLength + 1]; for (int i = 0; i < collectionNameLength; i++) newCollectionName[i] = collByteArray[i]; fieldList.Add(Helper.RoundToMultipleX(newCollectionName, 4)); // we have finish preparing packet head // let's put the content into packet fieldList.Add(Helper.RoundToMultipleX(insertor, 4)); // transform the list into byte[] byte[] msgInByteArray = Helper.ConcatByteArray(fieldList); if (logger.IsDebugEnabled) { StringWriter buff = new StringWriter(); foreach (byte by in msgInByteArray) { buff.Write(string.Format("{0:X}", by)); } logger.Debug("Insert Request string==>" + buff.ToString() + "<=="); } return msgInByteArray; }