public string PutTransform(string name, TransformerDefinition definition) { if (name == null) { throw new ArgumentNullException("name"); } if (definition == null) { throw new ArgumentNullException("definition"); } name = name.Trim(); var existingDefinition = IndexDefinitionStorage.GetTransformerDefinition(name); if (existingDefinition != null) { switch (existingDefinition.LockMode) { case TransformerLockMode.Unlock: if (existingDefinition.Equals(definition)) { return(name); // no op for the same transformer } break; case TransformerLockMode.LockedIgnore: Log.Info("Transformer {0} not saved because it was lock (with ignore)", name); return(name); default: throw new ArgumentOutOfRangeException(); } } var generator = IndexDefinitionStorage.CompileTransform(definition); if (existingDefinition != null) { IndexDefinitionStorage.RemoveTransformer(existingDefinition.TransfomerId); } var temporary = definition.Temporary; if (temporary) { definition.TransfomerId = Database.Transformers.GetNextTemporaryTransformerIndex(); IndexDefinitionStorage.CreateTransform(definition, generator); IndexDefinitionStorage.AddTransform(definition.TransfomerId, definition); } else { TransactionalStorage.Batch(accessor => { definition.TransfomerId = (int)Database.Documents.GetNextIdentityValueWithoutOverwritingOnExistingDocuments("TransformerId", accessor); }); IndexDefinitionStorage.CreateTransform(definition, generator); IndexDefinitionStorage.PersistTransform(definition); IndexDefinitionStorage.AddTransform(definition.TransfomerId, definition); TransactionalStorage.ExecuteImmediatelyOrRegisterForSynchronization(() => Database.Notifications.RaiseNotifications(new TransformerChangeNotification() { Name = name, Type = TransformerChangeTypes.TransformerAdded, })); } return(name); }
public string PutTransform(string name, TransformerDefinition definition, bool isReplication = false) { if (name == null) { throw new ArgumentNullException("name"); } if (definition == null) { throw new ArgumentNullException("definition"); } name = name.Trim(); var existingDefinition = IndexDefinitionStorage.GetTransformerDefinition(name); if (existingDefinition != null) { var newTransformerVersion = definition.TransformerVersion; var currentTransformerVersion = existingDefinition.TransformerVersion; // whether we update the transformer definition or not, // we need to update the transformer version existingDefinition.TransformerVersion = definition.TransformerVersion = Math.Max(currentTransformerVersion ?? 0, newTransformerVersion ?? 0); switch (isReplication) { case true: if (newTransformerVersion != null && currentTransformerVersion != null && newTransformerVersion <= currentTransformerVersion) { //this new transformer is an older version of the current one return(null); } // we need to update the lock mode only if it was updated by another server existingDefinition.LockMode = definition.LockMode; break; default: switch (existingDefinition.LockMode) { case TransformerLockMode.Unlock: if (existingDefinition.Equals(definition)) { return(name); // no op for the same transformer } definition.TransformerVersion++; break; case TransformerLockMode.LockedIgnore: Log.Info("Transformer {0} not saved because it was lock (with ignore)", name); return(name); default: throw new ArgumentOutOfRangeException(); } break; } } else if (isReplication == false) { // we're creating a new transformer, // we need to take the transformer version of the deleted transformer (if exists) definition.TransformerVersion = GetDeletedTransformerId(definition.Name); definition.TransformerVersion = (definition.TransformerVersion ?? 0) + 1; } if (definition.TransformerVersion == null) { definition.TransformerVersion = 0; } var generator = IndexDefinitionStorage.CompileTransform(definition); if (existingDefinition != null) { IndexDefinitionStorage.RemoveTransformer(existingDefinition.TransfomerId); } var temporary = definition.Temporary; if (temporary) { definition.TransfomerId = Database.Transformers.GetNextTemporaryTransformerIndex(); IndexDefinitionStorage.CreateTransform(definition, generator); IndexDefinitionStorage.AddTransform(definition.TransfomerId, definition); } else { TransactionalStorage.Batch(accessor => { definition.TransfomerId = (int)Database.Documents.GetNextIdentityValueWithoutOverwritingOnExistingDocuments("TransformerId", accessor); }); IndexDefinitionStorage.CreateTransform(definition, generator); IndexDefinitionStorage.PersistTransform(definition); IndexDefinitionStorage.AddTransform(definition.TransfomerId, definition); TransactionalStorage.ExecuteImmediatelyOrRegisterForSynchronization(() => Database.Notifications.RaiseNotifications(new TransformerChangeNotification() { Name = name, Type = TransformerChangeTypes.TransformerAdded, Version = definition.TransformerVersion })); } return(name); }