public void MarcRecordUtility_ParseAllFormat_2() { string host = "host"; Mock <IIrbisConnection> mock = new Mock <IIrbisConnection>(); mock.SetupGet(c => c.Host).Returns(host); IIrbisConnection connection = mock.Object; string database = "IBIS"; string[] lines = _GetLines(); MarcRecord[] records = MarcRecordUtility.ParseAllFormat ( database, connection, lines ); Assert.AreEqual(lines.Length, records.Length); for (int i = 0; i < records.Length; i++) { MarcRecord record = records[i]; Assert.AreEqual(host, record.HostName); Assert.AreEqual(database, record.Database); } }
public void MarcRecordUtility_ToJson_1() { MarcRecord record = new MarcRecord(); Assert.AreEqual("{\"fields\":[]}", MarcRecordUtility.ToJson(record).DosToUnix()); record = _GetRecord(); Assert.AreEqual("{\"fields\":[{\"tag\":700,\"subfields\":[{\"code\":\"a\",\"value\":\"Иванов\"},{\"code\":\"b\",\"value\":\"И. И.\"}]},{\"tag\":701,\"subfields\":[{\"code\":\"a\",\"value\":\"Петров\"},{\"code\":\"b\",\"value\":\"П. П.\"}]},{\"tag\":200,\"subfields\":[{\"code\":\"a\",\"value\":\"Заглавие\"},{\"code\":\"e\",\"value\":\"подзаголовочное\"},{\"code\":\"f\",\"value\":\"И. И. Иванов, П. П. Петров\"}]},{\"tag\":300,\"value\":\"Первое примечание\"},{\"tag\":300,\"value\":\"Второе примечание\"},{\"tag\":300,\"value\":\"Третье примечание\"}]}", MarcRecordUtility.ToJson(record).DosToUnix()); }
public void MarcRecordUtility_ParseAllFormat_1() { string host = "host"; Mock <IIrbisConnection> mock = new Mock <IIrbisConnection>(); mock.SetupGet(c => c.Host).Returns(host); IIrbisConnection connection = mock.Object; AbstractEngine executive = new StandardEngine(connection, null); mock.SetupGet(c => c.Executive).Returns(executive); string database = "IBIS"; string[] lines = _GetLines(); ResponseBuilder builder = new ResponseBuilder(); foreach (string line in lines) { builder.AppendUtf(line); builder.NewLine(); } byte[] rawAnswer = builder.Encode(); byte[] rawRequest = new byte[0]; ServerResponse response = new ServerResponse ( connection, rawAnswer, rawRequest, true ); MarcRecord[] records = MarcRecordUtility.ParseAllFormat ( database, response ); Assert.AreEqual(lines.Length, records.Length); for (int i = 0; i < records.Length; i++) { MarcRecord record = records[i]; Assert.AreEqual(host, record.HostName); Assert.AreEqual(database, record.Database); } }
public T[] ReadRecords <T> ( [CanBeNull] string database, [NotNull] IEnumerable <int> mfnList, [NotNull] Func <MarcRecord, T> func ) { Sure.NotNull(mfnList, "mfnList"); if (string.IsNullOrEmpty(database)) { database = Connection.Database; } database.ThrowIfNull("database"); int[] array = mfnList.ToArray(); if (array.Length == 0) { return(new T[0]); } if (array.Length == 1) { int mfn = array[0]; MarcRecord record = Connection.ReadRecord ( database, mfn, false, null ); T result1 = func(record); return(new[] { result1 }); } #if FW4 using (BlockingCollection <T> collection = new BlockingCollection <T>(array.Length)) { int[][] slices = array.Slice(1000).ToArray(); foreach (int[] slice in slices) { if (slice.Length == 1) { MarcRecord record = Connection.ReadRecord ( database, slice[0], false, null ); _records.Add(record); } else { FormatCommand command = Connection.CommandFactory .GetFormatCommand(); command.Database = database; command.FormatSpecification = IrbisFormat.All; command.MfnList.AddRange(slice); Connection.ExecuteCommand(command); string[] lines = command.FormatResult .ThrowIfNullOrEmpty ( "command.FormatResult" ); Debug.Assert ( lines.Length == slice.Length, "some records not retrieved" ); Parallel.ForEach ( lines, line => _ParseRecord ( line, database, func, collection ) ); } } collection.CompleteAdding(); return(collection.ToArray()); } #else FormatCommand command = Connection.CommandFactory.GetFormatCommand(); command.Database = database; command.FormatSpecification = IrbisFormat.All; command.MfnList.AddRange(array); if (array.Length > IrbisConstants.MaxPostings) { throw new ArgumentException(); } Connection.ExecuteCommand(command); MarcRecord[] records = MarcRecordUtility.ParseAllFormat ( database, Connection, command.FormatResult .ThrowIfNullOrEmpty("command.FormatResult") ); Debug.Assert ( command.MfnList.Count == records.Length, "some records not retrieved" ); T[] result = records.Select ( // ReSharper disable ConvertClosureToMethodGroup record => func(record) // ReSharper restore ConvertClosureToMethodGroup ) .ToArray(); return(result); #endif }