/// <summary> /// Gets Numbers the of events. /// </summary> /// <returns>The number of events.</returns> public long NumberOfEvents(string appID) { long count = 0; lock (_lock) { try { if (!keepOnlyOneConnection || connection == null) { connection = new FileConnection("URI=file:" + this.DBfileFullPath); connection.Open(); } count = connection.Tables[TABLE_NAME].Select($"{MA_APP_ID_COLUMN_NAME} = '{appID}'").Count(); UnityEngine.Debug.Log(string.Format("count = {0}", count)); connection.Sql.AcceptChanges(); } finally { if (!keepOnlyOneConnection && connection != null) { connection.Save(); connection.Dispose(); } } } return(count); }
/// <summary> /// Add an event to the store. /// </summary> /// <returns><c>true</c>, if event was put, <c>false</c> otherwise.</returns> public void PutEvent(string eventString, string appId) { bool proceedToInsert = false; long currentDatabaseSize = DatabaseSize; if (string.IsNullOrEmpty(appId)) { throw new ArgumentNullException("AppId"); } if (currentDatabaseSize >= _maConfig.MaxDBSize) { proceedToInsert = false; InvalidOperationException e = new InvalidOperationException(); _logger.Error(e, "The database size has exceeded the threshold limit. Unable to insert any new events"); } else if ((double)currentDatabaseSize / (double)_maConfig.MaxDBSize >= _maConfig.DBWarningThreshold) { proceedToInsert = true; _logger.InfoFormat("The database size is almost full"); } else { proceedToInsert = true; } //keep the lock as short as possible if (proceedToInsert) { lock (_lock) { try { if (!keepOnlyOneConnection || connection == null) { connection = new FileConnection("URI=file:" + this.DBfileFullPath); connection.Open(); } var eventRow = connection.Tables[TABLE_NAME].NewRow(); eventRow[EVENT_COLUMN_NAME] = eventString; eventRow[EVENT_ID_COLUMN_NAME] = Guid.NewGuid().ToString(); eventRow[MA_APP_ID_COLUMN_NAME] = appId; connection.Tables[TABLE_NAME].Rows.Add(eventRow); connection.Sql.AcceptChanges(); } finally { if (!keepOnlyOneConnection && connection != null) { connection.Save(); connection.Dispose(); } } } } }
/// <summary> /// Increments the delivery attempt. /// </summary> /// <returns>Success of operation</returns> /// <param name="rowIds">Row identifiers.</param> public bool IncrementDeliveryAttempt(List <string> rowIds) { bool success = false; lock (_lock) { try { if (!keepOnlyOneConnection || connection == null) { connection = new FileConnection("URI=file:" + this.DBfileFullPath); connection.Open(); } List <DataRow> rows = new List <DataRow>(); foreach (var rowId in rowIds) { var matchingRows = connection.Tables[TABLE_NAME].Select($"{EVENT_ID_COLUMN_NAME} = '{rowId}'"); if (matchingRows != null && matchingRows.Length > 0) { rows.AddRange(matchingRows); } } rows = rows.Distinct().ToList(); for (var i = 0; i < rows.Count; i++) { rows[i][EVENT_DELIVERY_ATTEMPT_COUNT_COLUMN_NAME] = (int)rows[i][EVENT_DELIVERY_ATTEMPT_COUNT_COLUMN_NAME] + 1; } connection.Sql.AcceptChanges(); } finally { if (!keepOnlyOneConnection && connection != null) { connection.Save(); connection.Dispose(); } } return(success); } }
/// <summary> /// Deletes a list of events. /// </summary> /// <returns><c>true</c>, if events was deleted, <c>false</c> otherwise.</returns> /// <param name="rowIds">Row identifiers.</param> public void DeleteEvent(List <string> rowIds) { lock (_lock) { try { if (!keepOnlyOneConnection || connection == null) { connection = new FileConnection("URI=file:" + this.DBfileFullPath); connection.Open(); } List <DataRow> rows = new List <DataRow>(); foreach (var rowId in rowIds) { var matchingRows = connection.Tables[TABLE_NAME].Select($"{EVENT_ID_COLUMN_NAME} = '{rowId}'"); if (matchingRows != null && matchingRows.Length > 0) { rows.AddRange(matchingRows); } } rows = rows.Distinct().ToList(); for (var i = 0; i < rows.Count; i++) { connection.Tables[TABLE_NAME].Rows.Remove(rows[i]); } connection.Sql.AcceptChanges(); } finally { if (!keepOnlyOneConnection && connection != null) { connection.Save(); connection.Dispose(); } } } }
/// <summary> /// Get All event from the Event Store /// </summary> /// <param name="appID">Appid.</param> /// <param name="maxAllowed">maximum number of events to fetch</param> /// <returns>All the events as a List of <see cref="ThirdParty.Json.LitJson.JsonData"/>.</returns> public List <JsonData> GetEvents(string appID, int maxAllowed) { List <JsonData> eventList = new List <JsonData>(); lock (_lock) { try { if (!keepOnlyOneConnection || connection == null) { connection = new FileConnection("URI=file:" + this.DBfileFullPath); connection.Open(); } var events = connection.Tables[TABLE_NAME].Select($"{MA_APP_ID_COLUMN_NAME} = '{appID}'").OrderBy(r => r[EVENT_DELIVERY_ATTEMPT_COUNT_COLUMN_NAME]).Take(maxAllowed); foreach (var eventRow in events) { JsonData data = new JsonData(); data["id"] = eventRow[EVENT_ID_COLUMN_NAME].ToString(); data["event"] = eventRow[EVENT_COLUMN_NAME].ToString(); data["appId"] = eventRow[MA_APP_ID_COLUMN_NAME].ToString(); eventList.Add(data); } connection.Sql.AcceptChanges(); } finally { if (!keepOnlyOneConnection && connection != null) { connection.Save(); connection.Dispose(); } } } return(eventList); }
/// <summary> /// Saves the database. /// </summary> public void SaveDatabase() { lock (_lock) { try { if (!keepOnlyOneConnection || connection == null) { connection = new FileConnection("URI=file:" + this.DBfileFullPath); connection.Open(); } connection.Save(); } finally { if (!keepOnlyOneConnection && connection != null) { connection.Save(); connection.Dispose(); } } } }
private void CreateOrOpenDatabase() { lock (_lock) { this.DBfileFullPath = System.IO.Path.Combine(AmazonHookedPlatformInfo.Instance.PersistentDataPath, dbFileName); if (!File.Exists(this.DBfileFullPath)) { string directory = Path.GetDirectoryName(this.DBfileFullPath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } FileConnection.CreateFile(this.DBfileFullPath); } try { if (!keepOnlyOneConnection || connection == null) { connection = new FileConnection("URI=file:" + this.DBfileFullPath); connection.Open(); } if (!connection.Tables.Contains(TABLE_NAME)) { var table = new DataTable(TABLE_NAME); table.AcceptChanges(); var eventColumn = new DataColumn { ColumnName = EVENT_COLUMN_NAME, DataType = typeof(string), AllowDBNull = false }; var eventIdColumn = new DataColumn { ColumnName = EVENT_ID_COLUMN_NAME, DataType = typeof(string), AllowDBNull = false, Unique = true }; var maAppIdColumn = new DataColumn { ColumnName = MA_APP_ID_COLUMN_NAME, DataType = typeof(string), AllowDBNull = false }; var eventDeiliveryAttemptCountColumn = new DataColumn { ColumnName = EVENT_DELIVERY_ATTEMPT_COUNT_COLUMN_NAME, DataType = typeof(int), AllowDBNull = false, DefaultValue = 0 }; table.Columns.Add(eventColumn); table.Columns.Add(eventIdColumn); table.Columns.Add(maAppIdColumn); table.Columns.Add(eventDeiliveryAttemptCountColumn); table.AcceptChanges(); connection.Tables.Add(table); } connection.Sql.AcceptChanges(); } finally { if (!keepOnlyOneConnection && connection != null) { connection.Save(); connection.Dispose(); } } } }