// // Copyright 2014-2015 Amazon.com, // Inc. or its affiliates. All Rights Reserved. // // SPDX-License-Identifier: Apache-2.0 // using System; using System.Text.RegularExpressions; using Amazon.CognitoIdentity; using Amazon.Runtime.Internal.Util; using System.Text; namespace Amazon.CognitoSync.SyncManager.Internal { /// /// A Utility class for all the dataset operations /// public static class DatasetUtils { /// /// Valid dataset name pattern /// public static readonly string DATASET_NAME_PATTERN = @"^[a-zA-Z0-9_.:-]{1,128}$"; /// /// Unknown identity id when the identity id is null /// public static readonly string UNKNOWN_IDENTITY_ID = "unknown"; /// /// Validates the dataset name. /// /// The dataset name. /// Dataset name. public static string ValidateDatasetName(string datasetName) { if (!Regex.IsMatch(datasetName, DATASET_NAME_PATTERN)) { throw new ArgumentException("Invalid dataset name"); } return datasetName; } /// /// Validates the record key. It must be non empty and its length must be no /// greater than 128. Otherwise {@link IllegalArgumentException} will be /// thrown. /// /// The record key. /// Key. public static string ValidateRecordKey(string key) { if (string.IsNullOrEmpty(key) || Encoding.UTF8.GetByteCount(key) > 128) { throw new ArgumentException("Invalid record key"); } return key; } /// /// A helper function to compute record size which equals the sum of the /// UTF-8 string length of record key and value. 0 if record is null. /// /// The record size. /// Record. public static long ComputeRecordSize(Record record) { if (record == null) { return 0; } return Encoding.UTF8.GetByteCount(record.Key) + Encoding.UTF8.GetByteCount(record.Value); } /// /// A helper function to get the identity id of the dataset from credentials /// provider. If the identity id is null, UNKNOWN_IDENTITY_ID will be /// returned. /// /// The Cognito Credentials. /// The identity identifier. public static string GetIdentityId(CognitoAWSCredentials credentials) { return string.IsNullOrEmpty(credentials.GetCachedIdentityId()) ? UNKNOWN_IDENTITY_ID : credentials.GetCachedIdentityId(); } /// /// A helper function to truncate a DateTime object to whole seconds. /// /// The truncated DateTime /// The DateTime to be truncated. public static DateTime TruncateToSeconds(DateTime dateTime) { return dateTime.AddTicks(-(dateTime.Ticks % TimeSpan.TicksPerSecond)); } } }