public override int Count(string objectFullName, ObjectFindOptions options, ObjectIndex[] indexes) { using (var db = OpenData()) { var parameters = new List<IDataParameter>(); var sql = _CreateSQLFromFindOptions(db, objectFullName, options, indexes, SQLStatements.RowsCount, parameters); return db.ExecuteScalar<int>(sql, 0, parameters.ToArray()); } }
private static string _CreateSQLFromFindOptions(IDatabaseService db, string objectFullName, ObjectFindOptions options, ObjectIndex[] indexes, string templateSql, IList<IDataParameter> parameters) { var logic = options.Logic; var oper = options.Operator; var limit = options.Limit; bool useOr = ObjectFindLogic.Or == logic; bool useLike = ObjectFindOperator.Like == oper; bool isNull = ObjectFindOperator.IsNull == oper; var limitSql = (0 == limit) ? SQLStatements.NoLimit : string.Format(SQLStatements.Limit, limit); var tableName = _CreateTableName(db, objectFullName); var sqlConstraint = new StringBuilder(); if (null != indexes && 0 < indexes.Length) { // initialize the where clause sqlConstraint.Append("WHERE "); for (int i = 0; indexes.Length > i; i++) { var idx = indexes[i]; // skip over null objects if (null == idx) { continue; } if (0 < i) { if (useOr) { sqlConstraint.Append(" OR "); } else { sqlConstraint.Append(" AND "); } } var paramName = "p" + i + idx.Name; var value = idx.GetObjectValue(); sqlConstraint.Append(db.MakeQuotedName(idx.Name)); if (useLike) { sqlConstraint.Append(' '); sqlConstraint.Append(db.MakeLikeParamReference(paramName)); parameters.Add(ObjectIndexProvider.MakeLikeParameter(db, paramName, value)); } else if (isNull) { sqlConstraint.Append(" IS NULL"); } else { sqlConstraint.Append(" = "); sqlConstraint.Append(db.MakeParamReference(paramName)); parameters.Add(db.MakeParam(paramName, value)); } } } var orderBySql = _CreateOrderBySQL(db, options.Order); return string.Format(templateSql, tableName, sqlConstraint.ToString(), orderBySql, limitSql); }
public void MultiThreadedObjectStoreGet() { var taskCount = 50; var taskList = new List<Task>(); using (var svc = new ObjectService(ObjectTestHelper.GetConfig())) { var ns = ObjectTestHelper.NameSpace1; var obj = ObjectTestHelper.ObjectName1; for (int i = 0; taskCount > i; i++) { taskList.Add(Task.Factory.StartNew((object state) => { lock (svc) { if (!svc.NameSpaceExists(ns)) { svc.CreateNameSpace(new ObjectNameSpaceConfig(ns, "ZeroG Test", "Unit Test", DateTime.Now)); } if (!svc.ObjectNameExists(ns, obj)) { svc.ProvisionObjectStore( new ObjectMetadata(ns, obj, new ObjectIndexMetadata[] { new ObjectIndexMetadata("IntIndex1", ObjectIndexType.Integer), new ObjectIndexMetadata("StrIndex1", ObjectIndexType.String, 15), new ObjectIndexMetadata("StrNullIndex1", ObjectIndexType.String, 5, true) })); } } var val1 = new Guid("{D22640F0-7D87-4F1C-8817-119FC036FAC1}"); var val2 = new Guid("{72FC1391-EC51-4826-890B-D02071A9A2DE}"); var val3 = new Guid("{72FC1391-EC51-4826-890B-D02071A9A2DE}"); var intIndex1 = 5; var intIndex2 = 12500; var intIndex3 = -100; var strIndex1 = "asdf"; var strIndex2 = "index test val"; var strIndex3 = "zzyyxx"; var strNullIndexVal = "0011"; var objID1 = svc.Store(ns, new PersistentObject() { Name = obj, Value = val1.ToByteArray(), Indexes = new ObjectIndex[] { ObjectIndex.Create("IntIndex1", intIndex1), ObjectIndex.Create("StrIndex1", strIndex1), ObjectIndex.Create("StrNullIndex1", null) } }); var objID2 = svc.Store(ns, new PersistentObject() { Name = obj, Value = val2.ToByteArray(), Indexes = new ObjectIndex[] { ObjectIndex.Create("IntIndex1", intIndex2), ObjectIndex.Create("StrIndex1", strIndex2), ObjectIndex.Create("StrNullIndex1", strNullIndexVal) } }); var objID3 = svc.Store(ns, new PersistentObject() { Name = obj, Value = val3.ToByteArray(), Indexes = new ObjectIndex[] { ObjectIndex.Create("IntIndex1", intIndex3), ObjectIndex.Create("StrIndex1", strIndex3), ObjectIndex.Create("StrNullIndex1", null) } }); var options = new ObjectFindOptions() { Operator = ObjectFindOperator.Equals, Logic = ObjectFindLogic.And }; var findVals = svc.Find(ns, obj, options, new ObjectIndex[] { ObjectIndex.Create("ID", objID3.ID), ObjectIndex.Create("StrIndex1", strIndex3) }).ToArray(); Assert.AreEqual(1, findVals.Length); Assert.AreEqual(val3, new Guid(findVals[0])); }, i, TaskCreationOptions.LongRunning)); } foreach (Task t in taskList) { t.Wait(); } var id = svc.GetNextObjectID(ns, obj); Assert.AreEqual(3 * taskCount + 1, id); Assert.AreEqual(3 * taskCount, svc.Iterate(ns, obj).Count()); // count all index values Assert.AreEqual(3 * taskCount, svc.Count(ns, obj)); } }
public override int[] Find(string objectFullName, ObjectFindOptions options, params ObjectIndex[] indexes) { int[] returnValue = null; using (var db = OpenData()) { var parameters = new List<IDataParameter>(); var sql = _CreateSQLFromFindOptions(db, objectFullName, options, indexes, SQLStatements.Find, parameters); returnValue = db.GetValues<int>(sql, parameters.ToArray()); } return returnValue; }
public override int[] Find(string objectFullName, ObjectFindOptions options, params ObjectIndex[] indexes) { int[] returnValue = null; var logic = options.Logic; var oper = options.Operator; var limit = options.Limit; bool useOr = ObjectFindLogic.Or == logic; bool useLike = ObjectFindOperator.Like == oper; bool isNull = ObjectFindOperator.IsNull == oper; using (var db = OpenData()) { var tableName = _CreateTableName(db, objectFullName); var topSql = (0 == limit) ? SQLStatements.NoTop : string.Format(SQLStatements.Top, limit); var parameters = new List<IDbDataParameter>(); var sqlConstraint = new StringBuilder(); if (null != indexes && 0 < indexes.Length) { // initialize the where clause sqlConstraint.Append("WHERE "); for (int i = 0; indexes.Length > i; i++) { var idx = indexes[i]; // skip over null objects if (null == idx) { continue; } if (0 < i) { if (useOr) { sqlConstraint.Append(" OR "); } else { sqlConstraint.Append(" AND "); } } var paramName = "p" + i + idx.Name; var value = idx.GetObjectValue(); sqlConstraint.Append(db.MakeQuotedName(idx.Name)); if (useLike) { sqlConstraint.Append(' '); sqlConstraint.Append(db.MakeLikeParamReference(paramName)); parameters.Add(ObjectIndexProvider.MakeLikeParameter(db, paramName, value)); } else if (isNull) { sqlConstraint.Append(" IS NULL"); } else { sqlConstraint.Append(" = "); sqlConstraint.Append(db.MakeParamReference(paramName)); parameters.Add(db.MakeParam(paramName, value)); } } } var orderBySql = _CreateOrderBySQL(db, options.Order); returnValue = db.GetValues<int>(string.Format(SQLStatements.Find, tableName, sqlConstraint.ToString(), topSql, orderBySql), parameters.ToArray()); } return returnValue; }
public override int Count(string objectFullName, ObjectFindOptions options, ObjectIndex[] indexes) { throw new NotImplementedException(); }
public void FindWithCachingTest() { using (var svc = new ObjectService(ObjectTestHelper.GetConfigWithCaching())) { var ns = ObjectTestHelper.NameSpace1; var obj = ObjectTestHelper.ObjectName1; svc.CreateNameSpace(new ObjectNameSpaceConfig(ns, "ZeroG Test", "Unit Test", DateTime.Now)); svc.ProvisionObjectStore( new ObjectMetadata(ns, obj, new ObjectIndexMetadata[] { new ObjectIndexMetadata("IntIndex1", ObjectIndexType.Integer), new ObjectIndexMetadata("StrIndex1", ObjectIndexType.String, 15) })); var val1 = new Guid("{D22640F0-7D87-4F1C-8817-119FC036FAC1}"); var val2 = new Guid("{72FC1391-EC51-4826-890B-D02071A9A2DE}"); var val3 = new Guid("{72FC1391-EC51-4826-890B-D02071A9A2DE}"); var intIndex1 = 5; var intIndex2 = 12500; var intIndex3 = -100; var strIndex1 = "asdf"; var strIndex2 = "index test val"; var strIndex3 = "zzyyxx"; var objID1 = svc.Store(ns, new PersistentObject() { Name = obj, Value = val1.ToByteArray(), Indexes = new ObjectIndex[] { ObjectIndex.Create("IntIndex1", intIndex1), ObjectIndex.Create("StrIndex1", strIndex1) } }); var objID2 = svc.Store(ns, new PersistentObject() { Name = obj, Value = val2.ToByteArray(), Indexes = new ObjectIndex[] { ObjectIndex.Create("IntIndex1", intIndex2), ObjectIndex.Create("StrIndex1", strIndex2) } }); var objID3 = svc.Store(ns, new PersistentObject() { Name = obj, Value = val3.ToByteArray(), Indexes = new ObjectIndex[] { ObjectIndex.Create("IntIndex1", intIndex3), ObjectIndex.Create("StrIndex1", strIndex3) } }); // test a single index lookup using And var options = new ObjectFindOptions() { Operator = ObjectFindOperator.Equals, Logic = ObjectFindLogic.And }; var findVals = svc.Find(ns, obj, options, new ObjectIndex[] { ObjectIndex.Create("IntIndex1", 12500) }).ToArray(); Assert.AreEqual(1, findVals.Length); Assert.AreEqual(val2, new Guid(findVals[0])); // test two index lookups using And options = new ObjectFindOptions() { Operator = ObjectFindOperator.Equals, Logic = ObjectFindLogic.And }; findVals = svc.Find(ns, obj, options, new ObjectIndex[] { ObjectIndex.Create("StrIndex1", "index test val"), ObjectIndex.Create("IntIndex1", 12500) }).ToArray(); Assert.AreEqual(1, findVals.Length); Assert.AreEqual(val2, new Guid(findVals[0])); // test a single lookup using Or options = new ObjectFindOptions() { Operator = ObjectFindOperator.Equals, Logic = ObjectFindLogic.Or }; findVals = svc.Find(ns, obj, options, new ObjectIndex[] { ObjectIndex.Create("IntIndex1", 12500) }).ToArray(); Assert.AreEqual(1, findVals.Length); Assert.AreEqual(val2, new Guid(findVals[0])); // test two index lookups using Or options = new ObjectFindOptions() { Operator = ObjectFindOperator.Equals, Logic = ObjectFindLogic.Or }; findVals = svc.Find(ns, obj, options, new ObjectIndex[] { ObjectIndex.Create("IntIndex1", 12500), ObjectIndex.Create("StrIndex1", "asdf") }).ToArray(); Assert.AreEqual(2, findVals.Length); var findVal1 = new Guid(findVals[0]); var findVal2 = new Guid(findVals[1]); Assert.IsFalse(findVal1 == findVal2); Assert.IsTrue(findVal1 == val1 || findVal1 == val2); Assert.IsTrue(findVal2 == val1 || findVal2 == val2); } }
public int Count(ObjectFindOptions options, ObjectIndex[] indexes) { return _service.Count(_nameSpace, _objectName, options, indexes); }
public byte[][] Find(ObjectFindOptions options, ObjectIndex[] indexes) { return _service.Find(_nameSpace, _objectName, options, indexes).ToArray(); }
public IEnumerable<byte[]> Find(string nameSpace, string objectName, ObjectFindOptions options, ObjectIndex[] indexes) { _ValidateArguments(nameSpace, objectName); var objectFullName = ObjectNaming.CreateFullObjectName(nameSpace, objectName); var objectIds = _objectIndexer.Find(objectFullName, options, indexes); foreach (var objectId in objectIds) { yield return _objectStore.Get(objectFullName, objectId); } }
public int Count(string nameSpace, string objectName, ObjectFindOptions options, ObjectIndex[] indexes) { _ValidateArguments(nameSpace, objectName); var objectFullName = ObjectNaming.CreateFullObjectName(nameSpace, objectName); return _objectIndexer.Count(objectFullName, options, indexes); }