public void CopyToDocument() { CustomValueType = new SampleValueType {TheValue = 1.34d}; var mapper = CreateMapper(); var doc = new Document(); mapper.CopyToDocument(this, doc); Assert.That(doc.GetFieldable("CustomValueType").TokenStreamValue.ToString(), Is.EqualTo("(numeric,valSize=64,precisionStep=4)")); Assert.That(doc.GetFieldable("CustomValueType").StringValue, Is.EqualTo(CustomValueType.TheValue.ToString())); }
public void CopyToDocument() { TimeStamp = new DateTime(2012, 4, 23); var mapper = CreateMapper(); var doc = new Document(); mapper.CopyToDocument(this, doc); Assert.That(doc.GetFieldable("TimeStamp").TokenStreamValue().ToString(), Is.EqualTo("(numeric,valSize=64,precisionStep=4)")); Assert.That(doc.GetFieldable("TimeStamp").StringValue(), Is.EqualTo(TimeStamp.ToUniversalTime().Ticks.ToString())); }
public void CopyToDocument() { TimeStampOffset = new DateTimeOffset(new DateTime(2012, 4, 23), TimeSpan.Zero); var mapper = CreateMapper(); var doc = new Document(); mapper.CopyToDocument(this, doc); Assert.That(doc.GetFieldable("TimeStampOffset").TokenStreamValue.ToString(), Is.EqualTo("(numeric,valSize=64,precisionStep=4)")); Assert.That(doc.GetFieldable("TimeStampOffset").StringValue, Is.EqualTo(TimeStampOffset.Value.UtcTicks.ToString())); }
public static IndexDocumentData FromDocument(Document doc) { return new IndexDocumentData() { Package = PackageJson.FromJson(JObject.Parse(doc.GetField("Data").StringValue)), Checksum = Int32.Parse(doc.GetFieldable("Checksum").StringValue), Feeds = doc.GetFields("CuratedFeed").Select(f => f.StringValue).ToList() }; }
internal static Term GetIdTerm(Document document) { var versionID = NumericUtils.IntToPrefixCoded(Int32.Parse((string)document.GetFieldable(LuceneManager.KeyFieldName).StringValue())); if (string.IsNullOrEmpty(versionID)) throw new ApplicationException("VersionID field missing"); var term = new Term(LuceneManager.KeyFieldName, versionID); return term; }
public void UsesPrecisionStep() { mapper = new NumericReflectionFieldMapper<Sample>(typeof(Sample).GetProperty("Int"), StoreMode.Yes, null, TypeDescriptor.GetConverter(typeof(int)), "Int", 128, 1.0f); var sample = new Sample { Long = 1234L }; var document = new Document(); mapper.CopyToDocument(sample, document); var field = document.GetFieldable("Int"); Assert.That(field.TokenStreamValue.ToString(), Is.EqualTo("(numeric,valSize=32,precisionStep=128)")); }
public void StoreLong() { mapper = new NumericReflectionFieldMapper<Sample>(typeof(Sample).GetProperty("Long"), StoreMode.Yes, null, TypeDescriptor.GetConverter(typeof(long)), "Long", NumericUtils.PRECISION_STEP_DEFAULT, 1.0f); var sample = new Sample { Long = 1234L }; var document = new Document(); mapper.CopyToDocument(sample, document); var field = document.GetFieldable("Long"); Assert.That(field.TokenStreamValue.ToString(), Is.EqualTo("(numeric,valSize=64,precisionStep=4)")); }
public void ThenIWantItToBeIndexedWithPrecisionStep() { // Arrange var document = new Document(); var input = DateTime.UtcNow; // Act document.Add(input).Indexed().WithPrecisionStep(8).As("Foo"); // Assert var field = document.GetFieldable("Foo"); Assert.True(field.IsIndexed); }
public void ThenIWantItToBeIndexed() { // Arrange var document = new Document(); var input = 5f; // Act document.Add(input).Indexed().As("Foo"); // Assert var field = document.GetFieldable("Foo"); Assert.True(field.IsIndexed); }
public void ThenIWantItToBeIndexedWithPrecisionStepWithBoost() { // Arrange var document = new Document(); var input = Int32.MaxValue; var boost = 2.0f; // Act document.Add(input).Indexed().WithPrecisionStep(8).BoostBy(boost).As("Foo"); // Assert var field = document.GetFieldable("Foo"); Assert.True(field.IsIndexed); Assert.Equal(boost, field.Boost); }
public void ThenIWantItToBeIndexedWithBoost() { // Arrange var document = new Document(); var input = DateTime.UtcNow; var boost = 2.0f; // Act document.Add(input).Indexed().BoostBy(boost).As("Foo"); // Assert var field = document.GetFieldable("Foo"); Assert.True(field.IsIndexed); Assert.Equal(boost, field.Boost); }
public override void OnIndexEntryCreated(string entryKey, Document document) { var resultDocId = document.GetField(setupDoc.DocumentKey); if (resultDocId == null) { log.Warn("Could not find document id property '{0}' in '{1}' for index '{2}'", setupDoc.DocumentKey, entryKey, index); return; } var documentId = resultDocId.StringValue; itemsToRemove.TryRemove(documentId); var resultDoc = database.Get(documentId, null); if (resultDoc == null) { log.Warn("Could not find a document with the id '{0}' for index '{1}'", documentId, index); return; } var entityName = resultDoc.Metadata.Value <string>(Constants.RavenEntityName); if (entityName != null && viewGenerator.ForEntityNames.Contains(entityName)) { log.Warn( "Rejected update for a potentially recursive update on document '{0}' because the index '{1}' includes documents with entity name of '{2}'", documentId, index, entityName); return; } if (viewGenerator.ForEntityNames.Count == 0) { log.Warn( "Rejected update for a potentially recursive update on document '{0}' because the index '{1}' includes all documents", documentId, index); return; } var changesMade = false; foreach (var mapping in setupDoc.FieldNameMappings) { var field = document.GetFieldable(mapping.Key + "_Range") ?? document.GetFieldable(mapping.Key); if (field == null) { continue; } var numericField = field as NumericField; if (numericField != null) { resultDoc.DataAsJson[mapping.Value] = new RavenJValue(numericField.NumericValue); } else { resultDoc.DataAsJson[mapping.Value] = field.StringValue; } changesMade = true; } if (changesMade) { database.Put(documentId, resultDoc.Etag, resultDoc.DataAsJson, resultDoc.Metadata, null); } }
public override void OnIndexEntryCreated(string entryKey, Document document) { var resultDocId = document.GetField(setupDoc.DocumentKey); if (resultDocId == null) { log.Warn("Could not find document id property '{0}' in '{1}' for index '{2}'", setupDoc.DocumentKey, entryKey, index); return; } var documentId = resultDocId.StringValue; itemsToRemove.Remove(documentId); var resultDoc = database.Get(documentId, null); if (resultDoc == null) { log.Warn("Could not find a document with the id '{0}' for index '{1}'", documentId, index); return; } var entityName = resultDoc.Metadata.Value<string>(Constants.RavenEntityName); if(entityName != null && viewGenerator.ForEntityNames.Contains(entityName)) { log.Warn( "Rejected update for a potentially recursive update on document '{0}' because the index '{1}' includes documents with entity name of '{2}'", documentId, index, entityName); return; } if(viewGenerator.ForEntityNames.Count == 0) { log.Warn( "Rejected update for a potentially recursive update on document '{0}' because the index '{1}' includes all documents", documentId, index); return; } var changesMade = false; foreach (var mapping in setupDoc.FieldNameMappings) { var field = document.GetFieldable(mapping.Key + "_Range") ?? document.GetFieldable(mapping.Key); if (field == null) continue; var numericField = field as NumericField; if (numericField != null) { resultDoc.DataAsJson[mapping.Value] = new RavenJValue(numericField.NumericValue); } else { resultDoc.DataAsJson[mapping.Value] = field.StringValue; } changesMade = true; } if (changesMade) database.Put(documentId, resultDoc.Etag, resultDoc.DataAsJson, resultDoc.Metadata, null); }
private string GetStringFromField(Document document, string propertyName) { var fieldable = document.GetFieldable(propertyName); var numericfield = document.GetFieldable(String.Concat(propertyName, "_Range")); if (numericfield != null) return String.Format(CultureInfo.InvariantCulture, "{0}", ((NumericField) numericfield).GetNumericValue()); if (fieldable == null) return String.Empty; var stringValue = fieldable.StringValue(); if (datePattern.IsMatch(stringValue)) { try { return DateTools.StringToDate(stringValue).ToString("u"); } catch { } } return stringValue; }
private object GetObjectFromField(Document document, string propertyName) { var fieldable = document.GetFieldable(propertyName); var numericfield = document.GetFieldable(String.Concat(propertyName, "_Range")); if (numericfield != null) return ((NumericField)numericfield).GetNumericValue(); if (fieldable == null) return null; var stringValue = fieldable.StringValue(); if (datePattern.IsMatch(stringValue)) { try { return DateTools.StringToDate(stringValue); } catch { } } return stringValue; }
private void assertCompressedFields29(Directory dir, bool shouldStillBeCompressed) { int count = 0; int TEXT_PLAIN_LENGTH = TEXT_TO_COMPRESS.Length * 2; // FieldSelectorResult.SIZE returns 2*number_of_chars for String fields: int BINARY_PLAIN_LENGTH = BINARY_TO_COMPRESS.Length; IndexReader reader = IndexReader.Open(dir, true); try { // look into sub readers and check if raw merge is on/off var readers = new System.Collections.Generic.List <IndexReader>(); ReaderUtil.GatherSubReaders(readers, reader); foreach (IndexReader ir in readers) { FieldsReader fr = ((SegmentReader)ir).GetFieldsReader(); Assert.IsTrue(shouldStillBeCompressed != fr.CanReadRawDocs(), "for a 2.9 index, FieldsReader.canReadRawDocs() must be false and other way round for a trunk index"); } // test that decompression works correctly for (int i = 0; i < reader.MaxDoc; i++) { if (!reader.IsDeleted(i)) { Document d = reader.Document(i); if (d.Get("content3") != null) { continue; } count++; IFieldable compressed = d.GetFieldable("compressed"); if (int.Parse(d.Get("id")) % 2 == 0) { Assert.IsFalse(compressed.IsBinary); Assert.AreEqual(TEXT_TO_COMPRESS, compressed.StringValue, "incorrectly decompressed string"); } else { Assert.IsTrue(compressed.IsBinary); Assert.IsTrue(BINARY_TO_COMPRESS.SequenceEqual(compressed.GetBinaryValue()), "incorrectly decompressed binary"); } } } //check if field was decompressed after optimize for (int i = 0; i < reader.MaxDoc; i++) { if (!reader.IsDeleted(i)) { Document d = reader.Document(i, new AnonymousFieldSelector()); if (d.Get("content3") != null) { continue; } count++; // read the size from the binary value using BinaryReader (this prevents us from doing the shift ops ourselves): // ugh, Java uses Big-Endian streams, so we need to do it manually. byte[] encodedSize = d.GetFieldable("compressed").GetBinaryValue().Take(4).Reverse().ToArray(); int actualSize = BitConverter.ToInt32(encodedSize, 0); int compressedSize = int.Parse(d.Get("compressedSize")); bool binary = int.Parse(d.Get("id")) % 2 > 0; int shouldSize = shouldStillBeCompressed ? compressedSize : (binary ? BINARY_PLAIN_LENGTH : TEXT_PLAIN_LENGTH); Assert.AreEqual(shouldSize, actualSize, "size incorrect"); if (!shouldStillBeCompressed) { Assert.IsFalse(compressedSize == actualSize, "uncompressed field should have another size than recorded in index"); } } } Assert.AreEqual(34 * 2, count, "correct number of tests"); } finally { reader.Dispose(); } }
public override void OnIndexEntryCreated(string entryKey, Document document) { using (var cmd = Connection.CreateCommand()) { cmd.Transaction = tx; var pkParam = cmd.CreateParameter(); pkParam.ParameterName = GetParameterName("entryKey"); pkParam.Value = entryKey; cmd.Parameters.Add(pkParam); var sb = new StringBuilder("INSERT INTO ") .Append(destination.TableName) .Append(" (") .Append(destination.PrimaryKeyColumnName) .Append(", "); foreach (var mapping in destination.ColumnsMapping) { sb.Append(mapping.Value).Append(", "); } sb.Length = sb.Length - 2; sb.Append(") \r\nVALUES (") .Append(pkParam.ParameterName) .Append(", "); foreach (var mapping in destination.ColumnsMapping) { var parameter = cmd.CreateParameter(); parameter.ParameterName = GetParameterName(mapping.Key); var field = document.GetFieldable(mapping.Key); var numericfield = document.GetFieldable(String.Concat(mapping.Key, "_Range")); if (numericfield != null) { field = numericfield; } if (field == null || field.StringValue == Constants.NullValue) { parameter.Value = DBNull.Value; } else if (field is NumericField) { var numField = (NumericField)field; parameter.Value = numField.NumericValue; } else { var stringValue = field.StringValue; if (datePattern.IsMatch(stringValue)) { try { parameter.Value = DateTools.StringToDate(stringValue); } catch { parameter.Value = stringValue; } } else { DateTime time; if (DateTime.TryParseExact(stringValue, Default.DateTimeFormatsToRead, CultureInfo.InvariantCulture, DateTimeStyles.None, out time)) { parameter.Value = time; } else { parameter.Value = stringValue; } } } cmd.Parameters.Add(parameter); sb.Append(parameter.ParameterName).Append(", "); } sb.Length = sb.Length - 2; sb.Append(")"); cmd.CommandText = sb.ToString(); cmd.ExecuteNonQuery(); } }
public override void OnIndexEntryCreated(string entryKey, Document document) { using (var cmd = Connection.CreateCommand()) { cmd.Transaction = tx; var pkParam = cmd.CreateParameter(); pkParam.ParameterName = GetParameterName("entryKey"); pkParam.Value = entryKey; cmd.Parameters.Add(pkParam); var sb = new StringBuilder("INSERT INTO ") .Append(destination.TableName) .Append(" (") .Append(destination.PrimaryKeyColumnName) .Append(", "); foreach (var mapping in destination.ColumnsMapping) { sb.Append(mapping.Value).Append(", "); } sb.Length = sb.Length - 2; sb.Append(") \r\nVALUES (") .Append(pkParam.ParameterName) .Append(", "); foreach (var mapping in destination.ColumnsMapping) { var parameter = cmd.CreateParameter(); parameter.ParameterName = GetParameterName(mapping.Key); var field = document.GetFieldable(mapping.Key); var numericfield = document.GetFieldable(String.Concat(mapping.Key, "_Range")); if (numericfield != null) field = numericfield; if (field == null || field.StringValue == Constants.NullValue) parameter.Value = DBNull.Value; else if (field is NumericField) { var numField = (NumericField)field; parameter.Value = numField.NumericValue; } else { var stringValue = field.StringValue; if (datePattern.IsMatch(stringValue)) { try { parameter.Value = DateTools.StringToDate(stringValue); } catch { parameter.Value = stringValue; } } else { DateTime time; if (DateTime.TryParseExact(stringValue, Default.DateTimeFormatsToRead, CultureInfo.InvariantCulture, DateTimeStyles.None, out time)) { parameter.Value = time; } else { parameter.Value = stringValue; } } } cmd.Parameters.Add(parameter); sb.Append(parameter.ParameterName).Append(", "); } sb.Length = sb.Length - 2; sb.Append(")"); cmd.CommandText = sb.ToString(); cmd.ExecuteNonQuery(); } }
public override void OnIndexEntryCreated(string indexName, string entryKey, Document document) { using (var cmd = Connection.CreateCommand()) { cmd.Transaction = tx; var pkParam = cmd.CreateParameter(); pkParam.ParameterName = GetParameterName("entryKey"); pkParam.Value = entryKey; cmd.Parameters.Add(pkParam); var sb = new StringBuilder("INSERT INTO ") .Append(destination.TableName) .Append(" (") .Append(destination.PrimaryKeyColumnName) .Append(", "); foreach (var mapping in destination.ColumnsMapping) { sb.Append(mapping.Value).Append(", "); } sb.Length = sb.Length - 2; sb.Append(") \r\nVALUES (") .Append(pkParam.ParameterName) .Append(", "); foreach (var mapping in destination.ColumnsMapping) { var parameter = cmd.CreateParameter(); parameter.ParameterName = GetParameterName(mapping.Key); var field = document.GetFieldable(mapping.Key); if (field == null) { parameter.Value = DBNull.Value; } else if (field is NumericField) { var numField = (NumericField)field; parameter.Value = numField.GetNumericValue(); } else { var stringValue = field.StringValue(); if (datePattern.IsMatch(stringValue)) { try { parameter.Value = DateTools.StringToDate(stringValue); } catch { parameter.Value = stringValue; } } else { parameter.Value = stringValue; } } cmd.Parameters.Add(parameter); sb.Append(parameter.ParameterName).Append(", "); } sb.Length = sb.Length - 2; sb.Append(")"); cmd.CommandText = sb.ToString(); cmd.ExecuteNonQuery(); } }
public override void OnIndexEntryCreated(string indexName, string entryKey, Document document) { using (var cmd = Connection.CreateCommand()) { cmd.Transaction = tx; var pkParam = cmd.CreateParameter(); pkParam.ParameterName = GetParameterName("entryKey"); pkParam.Value = entryKey; cmd.Parameters.Add(pkParam); var sb = new StringBuilder("INSERT INTO ") .Append(destination.TableName) .Append(" (") .Append(destination.PrimaryKeyColumnName) .Append(", "); foreach (var mapping in destination.ColumnsMapping) { sb.Append(mapping.Value).Append(", "); } sb.Length = sb.Length - 2; sb.Append(") \r\nVALUES (") .Append(pkParam.ParameterName) .Append(", "); foreach (var mapping in destination.ColumnsMapping) { var parameter = cmd.CreateParameter(); parameter.ParameterName = GetParameterName(mapping.Key); var field = document.GetFieldable(mapping.Key); if (field == null) parameter.Value = DBNull.Value; else if(field is NumericField) { var numField = (NumericField) field; parameter.Value = numField.GetNumericValue(); } else parameter.Value = field.StringValue(); cmd.Parameters.Add(parameter); sb.Append(parameter.ParameterName).Append(", "); } sb.Length = sb.Length - 2; sb.Append(")"); cmd.CommandText = sb.ToString(); cmd.ExecuteNonQuery(); } }