text:
<!DOCTYPE html>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />

/** Provides an interface to platform specific Idle functionality. */
define(function(require, exports, module) {
    var dr = require('$LIB/dr/dr.js'),
        JS = require('$LIB/jsclass.js'),
        globalScope = require('$SPRITE/global.js'),
        sprite = require('$SPRITE/sprite.js');

    module.exports = sprite.GlobalIdle = new JS.Class('sprite.GlobalIdle', {
        // Constructor /////////////////////////////////////////////////////////////
        /** The standard JSClass initializer function. Subclasses should not
            override this function.
            @param attrs:object A map of attribute names and values.
            @returns void */
        initialize: function(view, attrs) {
            this.view = view;

            // Setup callback function
            var self = this;
            this.__event = {};
            this.__doIdle = function doIdle(time) {
                var lastTime = self.lastTime;
                if (lastTime !== -1) {
                    time = Math.round(time);
                    var event = self.__event;
                    event.delta = time - lastTime;
                    event.time = time;
                    view.sendEvent('onidle', event);
                }
                self.lastTime = time;
            };
        },

        // Methods /////////////////////////////////////////////////////////////////
        start: function() {
			this.stop();
            this.lastTime = -1;
			this.theTimer = setInterval(this.__doIdle.bind(this), 20)
        },

        stop: function() {
			if (this.theTimer) {
				clearInterval(this.theTimer);
				this.theTimer = null;
			}
        }
    });
});


text:
package org.apache.storm.starter.streams;

import org.apache.storm.Config;
import org.apache.storm.StormSubmitter;
import org.apache.storm.streams.Pair;
import org.apache.storm.streams.PairStream;
import org.apache.storm.streams.StreamBuilder;
import org.apache.storm.streams.operations.StateUpdater;
import org.apache.storm.streams.operations.mappers.ValueMapper;
import org.apache.storm.streams.windowing.TumblingWindows;
import org.apache.storm.testing.TestWordSpout;
import org.apache.storm.topology.base.BaseWindowedBolt;

/**
 * A stateful word count that uses {@link PairStream#updateStateByKey(StateUpdater)} to
 * save the counts in a key value state. This example uses Redis state store.
 *
 *
You should start a local redis instance before running the 'storm jar' command. By default
 * the connection will be attempted at localhost:6379. The default
 * RedisKeyValueStateProvider parameters can be overridden in conf/storm.yaml, for e.g.
 *
 *

 * topology.state.provider.config: '{"keyClass":"...", "valueClass":"...",
 *  "keySerializerClass":"...", "valueSerializerClass":"...",
 *  "jedisPoolConfig":{"host":"localhost", "port":6379,
 *  "timeout":2000, "database":0, "password":"xyz"}}'
 *

 */
public class StatefulWordCount {
    public static void main(String[] args) throws Exception {
        StreamBuilder builder = new StreamBuilder();
        // a stream of words
        builder.newStream(new TestWordSpout(), new ValueMapper<String>(0), 2)
               .window(TumblingWindows.of(BaseWindowedBolt.Duration.seconds(2)))
               /*
                * create a stream of (word, 1) pairs
                */
               .mapToPair(w -> Pair.of(w, 1))
               /*
                * compute the word counts in the last two second window
                */
               .countByKey()
               /*
                * update the word counts in the state.
                * Here the first argument 0L is the initial value for the state
                * and the second argument is a function that adds the count to the current value in the state.
                */
               .updateStateByKey(0L, (state, count) -> state + count)
               /*
                * convert the state back to a stream and print the results
                */
               .toPairStream()
               .print();

        Config config = new Config();
        // use redis based state store for persistence
        config.put(Config.TOPOLOGY_STATE_PROVIDER, "org.apache.storm.redis.state.RedisKeyValueStateProvider");
        String topoName = "test";
        if (args.length > 0) {
            topoName = args[0];
        }
        config.setNumWorkers(1);
        StormSubmitter.submitTopologyWithProgressBar(topoName, config, builder.build());
    }
}

redpajama_set_name: RedPajamaGithub


text:
package org.datavec.api.records;

import org.datavec.api.records.metadata.RecordMetaData;
import org.datavec.api.writable.Writable;

import java.io.Serializable;
import java.util.List;

/**
 * A Record contains a set of values for a single example or instance. Each value in the Record is represented by
 * a {@link Writable} object. The record may (optionally) also have a {@link RecordMetaData} instance, that represents
 * metadata (source location, etc) for the record.<br>
 * For sequences, see {@link SequenceRecord}
 *
 * @author Alex Black
 */
public interface Record extends Serializable {

    /**
     * Get the record values, as a {@code List<Writable>}
     *
     * @return Record values
     */
    List<Writable> getRecord();

    /**
     * Get the record values for this Record
     */
    void setRecord(List<Writable> record);

    /**
     * Get the RecordMetaData for this record
     *
     * @return Metadata for this record (or null, if none has been set)
     */
    RecordMetaData getMetaData();

    /**
     * Set the Record metadata
     */
    void setMetaData(RecordMetaData recordMetaData);

}


text:
The Notifications tab will show you the list of notifications.
In general, the notifications will be information which will require some further actions from you.


text:
namespace LODS.LabClients.MVCSample.Exceptions {
    /// <summary>
    /// The base launch exception for all lab-related exceptions.
    /// </summary>
    public class LodException: System.Exception {
        /// <summary>
        /// The base launch exception for all lab-related exceptions.
        /// </summary>
        public LodException(string message) : base(message) { }
    }
}


text:
package org.apache.camel.component.github.consumer;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.github.GitHubComponent;
import org.apache.camel.component.github.GitHubComponentTestBase;
import org.eclipse.egit.github.core.RepositoryTag;
import org.junit.Test;

public class TagConsumerTest extends GitHubComponentTestBase {

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                context.addComponent("github", new GitHubComponent());
                from("github://tag?" + GITHUB_CREDENTIALS_STRING)
                        .process(new RepositoryTagProcessor())
                        .to(mockResultEndpoint);
            }
        };
    }

    @Test
    public void tagConsumerTest() throws Exception {
        RepositoryTag tag1 = repositoryService.addTag("TAG1");
        RepositoryTag tag2 = repositoryService.addTag("TAG2");
        RepositoryTag tag3 = repositoryService.addTag("TAG3");
        mockResultEndpoint.expectedBodiesReceivedInAnyOrder(tag1, tag2, tag3);
        Thread.sleep(1 * 1000);

        mockResultEndpoint.assertIsSatisfied();
    }

    public class RepositoryTagProcessor implements Processor {
        @Override
        public void process(Exchange exchange) throws Exception {
            Message in = exchange.getIn();
            RepositoryTag tag = (RepositoryTag) in.getBody();
            log.debug("Got TAG  [" + tag.getName() + "]");
        }
    }
}


text:
from yabgp.tlv import TLV
from ..linkstate import LinkState

@LinkState.register()
class NodeName(TLV):
    """
    node name
    """
    TYPE = 1026
    TYPE_STR = 'node_name'

    @classmethod
    def unpack(cls, data):

        return cls(value=data.decode('ascii'))


text:
module Fog
  module Identity
    class OpenStack
      class Real

        def delete_user(user_id)
          request(
            :expects => [200, 204],
            :method => 'DELETE',
            :path   => "users/#{user_id}"
          )
        end

      end

      class Mock

        def delete_user(user_id)
          self.data[:users].delete(
            list_users.body['users'].find {|x| x['id'] == user_id }['id'])

          response = Excon::Response.new
          response.status = 204
          response
        rescue
          raise Fog::Identity::OpenStack::NotFound
        end

      end
    end
  end
end


text:
require 'xctf_time/version' # rubocop:disable FileName
require 'omniauth/strategies/xctf_time'


text:
package pro.taskana.classification.rest.models;

import javax.validation.constraints.NotNull;
import org.springframework.hateoas.RepresentationModel;

import pro.taskana.classification.api.models.ClassificationSummary;

/** EntityModel class for {@link ClassificationSummary}. */
public class ClassificationSummaryRepresentationModel
    extends RepresentationModel<ClassificationSummaryRepresentationModel> {

  /** Unique Id. */
  @NotNull protected String classificationId;
  /**
   * The key of the Classification. This is typically an externally known code or abbreviation of
   * the Classification.
   */
  @NotNull protected String key;
  /**
   * The logical name of the entry point. This is needed by the task list application to determine
   * the redirect to work on a task of this Classification.
   */
  protected String applicationEntryPoint;
  /**
   * The category of the classification. Categories can be configured in the file
   * 'taskana.properties'.
   */
  @NotNull protected String category;
  /** The domain for which this classification is specified. */
  protected String domain;
  /** The name of the classification. */
  @NotNull protected String name;
  /** The Id of the parent classification. Empty string ("") if this is a root classification. */
  protected String parentId;
  /** The key of the parent classification. Empty string ("") if this is a root classification. */
  protected String parentKey;
  /** The priority of the classification. */
  @NotNull protected int priority;
  /**
   * The service level of the classification.
   *
   * <p>This is stated according to ISO 8601.
   */
  @NotNull protected String serviceLevel;
  /** The type of classification. Types can be configured in the file 'taskana.properties'. */
  protected String type;
  /** A custom property with name "1". */
  protected String custom1;
  /** A custom property with name "2". */
  protected String custom2;
  /** A custom property with name "3". */
  protected String custom3;
  /** A custom property with name "4". */
  protected String custom4;
  /** A custom property with name "5". */
  protected String custom5;
  /** A custom property with name "6". */
  protected String custom6;
  /** A custom property with name "7". */
  protected String custom7;
  /** A custom property with name "8". */
  protected String custom8;

  public String getClassificationId() {
    return classificationId;
  }

  public void setClassificationId(String classificationId) {
    this.classificationId = classificationId;
  }

  public String getKey() {
    return key;
  }

  public void setKey(String key) {
    this.key = key;
  }

  public String getParentId() {
    return parentId;
  }

  public void setParentId(String parentId) {
    this.parentId = parentId;
  }

  public String getParentKey() {
    return parentKey;
  }

  public void setParentKey(String parentKey) {
    this.parentKey = parentKey;
  }

  public String getCategory() {
    return category;
  }

  public void setCategory(String category) {
    this.category = category;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public String getDomain() {
    return domain;
  }

  public void setDomain(String domain) {
    this.domain = domain;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getPriority() {
    return priority;
  }

  public void setPriority(int priority) {
    this.priority = priority;
  }

  public String getApplicationEntryPoint() {
    return applicationEntryPoint;
  }

  public void setApplicationEntryPoint(String applicationEntryPoint) {
    this.applicationEntryPoint = applicationEntryPoint;
  }

  public String getServiceLevel() {
    return serviceLevel;
  }

  public void setServiceLevel(String serviceLevel) {
    this.serviceLevel = serviceLevel;
  }

  public String getCustom1() {
    return custom1;
  }

  public void setCustom1(String custom1) {
    this.custom1 = custom1;
  }

  public String getCustom2() {
    return custom2;
  }

  public void setCustom2(String custom2) {
    this.custom2 = custom2;
  }

  public String getCustom3() {
    return custom3;
  }

  public void setCustom3(String custom3) {
    this.custom3 = custom3;
  }

  public String getCustom4() {
    return custom4;
  }

  public void setCustom4(String custom4) {
    this.custom4 = custom4;
  }

  public String getCustom5() {
    return custom5;
  }

  public void setCustom5(String custom5) {
    this.custom5 = custom5;
  }

  public String getCustom6() {
    return custom6;
  }

  public void setCustom6(String custom6) {
    this.custom6 = custom6;
  }

  public String getCustom7() {
    return custom7;
  }

  public void setCustom7(String custom7) {
    this.custom7 = custom7;
  }

  public String getCustom8() {
    return custom8;
  }

  public void setCustom8(String custom8) {
    this.custom8 = custom8;
  }
}


text:
a.tableClicked = function(tableName, obj) {
	var table = this.tables[tableName];
	if (table && table.clicked)
		table.clicked(obj);
};

/**
 * Function to be called on click of table header.
 *
 * @function tableHeaderClicked
 * @lends ExilityPage
 * @param {Object}
 *            event - The onclick event from table header th.
 * @param {String}
 *            tableName - The name of the table.
 * @param {columnName}
 *            columnName -The name of the column in table.
 * @author vinay 03-August-2013
 */
a.tableHeaderClicked = function(event, tableName, columnName) {
	// get the target element where it is clicked(th or img).
	var obj = event.target;
	// get the custom attribute data-exilIconType from the obj
	var imageType = obj.getAttribute("data-exilImageType");

	if (imageType == 'filter')
		this.filterTable(obj, tableName, columnName);
	else if (imageType == 'sort')
		this.sortTable(obj, tableName, columnName);
	else
		this.sortTable(null, tableName, columnName);// default is sort.
};

a.sortTable = function(obj, tableName, columnName) {
	this.tables[tableName].sort(obj, columnName);
};

a.rowSelected = function(tableName, obj) {
	var table = this.tables[tableName];
	if (table)
		table.rowSelected(obj);
};

a.listMouseOver = function(obj, tableName, e) {
	if (!e) {
		e = window.event;
	}
	this.tables[tableName].mouseOver(obj, e);
};

a.listMouseOut = function(obj, tableName, e) {
	if (!e) {
		e = window.event;
	}
	this.tables[tableName].mouseOut(obj, e);
};

a.listClicked = function(obj, tableName, evt) {
	this.tables[tableName].clicked(obj, evt);
};

a.listDblClicked = function(obj, tableName, actionName) {
	this.tables[tableName].dblClicked(obj, actionName);
};

a.addTableRow = function(obj, tableName) {
	this.addRowTimer = null; // it could have been triggered with a timer
	// that was set when tab was pressed
	var table = this.tables[tableName];
	var f = table.functionBeforeAddRow;
	var result = true;
	if (f) {
		try {
			if (f.indexOf('(') >= 0) {
				eval('result = this.win.' + f);
			} else {
				result = this.win[f]();
			}
		} catch (e) {
			//
		}
		if (!result)
			return;
	}

	// var key = obj ? obj.key : null;
	// table.addRows(1, obj && key, true);
	var key = obj ? obj.key : table.getKey();
	// Dec 09 2009 : Bug ID 798 - In single selction value-inside grid -
	// Exility(Start): Vijay
	if ((table.dataForNewRowToBeClonedFromRow && table.dataForNewRowToBeClonedFromRow != 'none')
			|| table.dataForNewRowToBeClonedFromFirstRow
			|| table.rowsCanBeCloned) // Feb 16 2010 : Bug ID 967 - Drop down
		// issue in Grid - Exility : Venkat
		table.cloneFlag = 1;
	else
		table.cloneFlag = 0;
	// Dec 09 2009 : Bug ID 798 - In single selction value-inside grid -
	// Exility(End) : Vijay
	table.addRows(1, key, true);
	f = table.functionAfterAddRow;
	if (f) {
		try {
			if (f.indexOf('(') >= 0) // use has typed a funciton with
				// parameters
				eval('this.win.' + f);
			else
				// it is name of a function
				this.win[f]();
		} catch (e) {
			debug(f
					+ ' could not be run as a function. An exception occurred: '
					+ e);
		}
	}
};

a.cloneTableRow = function(obj, tableName) {
	var table = this.tables[tableName];
	var f = table.functionBeforeAddRow;
	var result = true;
	if (f) {
		try {
			if (f.indexOf('(') >= 0) {
				eval('result = this.win.' + f);
			} else {
				result = this.wan[f]();
			}
		} catch (e) {
			//
		}
		if (!result)
			return;
	}
	if (this.tables[tableName].dataForNewRowToBeClonedFromFirstRow
			|| this.tables[tableName].rowsCanBeCloned)
		this.tables[tableName].cloneFlag = 1;
	else
		this.tables[tableName].cloneFlag = 0;

	this.tables[tableName].cloneRow();
	f = table.functionAfterAddRow;
	if (f) {
		try {
			if (f.indexOf('(') >= 0) {
				eval('this.win.' + f);
			} else {
				this.win[f]();
			}
		} catch (e) {
			//
		}
	}
};

a.removeTableRows = function(obj, tableName) {
	this.tables[tableName].removeTableRows(tableName);
};

a.removeTableRow = function(evt, obj, tableName) {
	var curTable = this.tables[tableName];
	var tr = getRowInTable(obj);
	var idx = tr.rowIdx;
	var dataRow = curTable.grid[idx];
	if (dataRow.isDeleted) {
		this.setLastModifiedNode(obj);
		var result = true;
		var f = curTable.functionBeforeDeleteRow;
		if (f) {
			try {
				if (f.indexOf('(') >= 0) {
					eval('result = this.win.' + f);
				} else {
					result = this.win[f]();
				}
			} catch (ex) {
				//
			}
			if (!result)
				return false;
		}
		result = curTable.deleteRow(obj);
		if (!result) {
			if (evt && evt.preventDefault)
				evt.preventDefault();
			return false;
		}

		f = curTable.functionAfterDeleteRow;
		if (f) {
			try {
				if (f.indexOf('(') >= 0) {
					eval('result = this.win.' + f);
				} else {
					result = this.win[f]();
				}
			} catch (e) {
				//
			}
		}
		return result;
	}
	return true;
};

a.deleteAllTableRows = function(obj, tableName) {
	this.tables[tableName].deleteAllTableRows(!obj.checked);
};

a.deleteTableRow = function(evt, obj, tableName) {
	this.setLastModifiedNode(obj);
	var result = true;
	var table = this.tables[tableName];
	var f = table.functionBeforeDeleteRow;
	if (f) {
		try {
			if (f.indexOf('(') >= 0) {
				eval('result = this.win.' + f);
			} else {
				result = this.win[f]();
			}
		} catch (e) {
			//
		}
		if (!result)
			return;
	}

	result = table.deleteRow(obj);
	if (!result) {
		if (evt && evt.preventDefault)
			evt.preventDefault();
		return result;
	}

	f = table.functionAfterDeleteRow;
	if (f) {
		a.appendRow = function(tblNAme, dataRow) {
			this.tables[tblName].appendRow(dataRow);
		};

		a.insertRow = function(tblName, idx) {
			this.tables[tblName].insertRow(idx);
		};

		try {
			if (f.indexOf('(') >= 0) {
				eval('result = this.win.' + f);
			} else {
				result = this.win[f]();
			}
		} catch (e) {
			//
		}
	}
	return result;
};

a.paginate = function(tableName, action) {
	this.tables[tableName].paginate(action);
};

a.handleGridNav = function(obj, fieldName, e) {
	var field = this.fields[fieldName];
	var keycode = e.keyCode || e.which;

	if (keycode == 40 && e.altKey) {
		if (field && field.table) {
			var curTable = field.table;
			var curNbrRows = curTable.nbrRows;
			var curRow = obj.rowIdx;
			if (obj.name && obj.name.match(/.*Radio$/)) {
				curRow = this.doc
						.getElementById(obj.name.replace(/Radio$/, '')).rowIdx;
			}
			if (curRow < curNbrRows) {
				curRow++;
				var fieldIdToFocus = curTable.getObjectId(fieldName, curRow);
				var fieldToFocus = this.doc.getElementById(fieldIdToFocus);
				if (fieldToFocus) {
					if (obj.name && obj.name.match(/.*Radio$/)) {
						if (fieldToFocus.getElementsByTagName("input")
								&& fieldToFocus.getElementsByTagName("input")[0]) {
							fieldToFocus.getElementsByTagName("input")[0]
									.focus();
						}
					} else if (obj.className
							&& obj.className == 'checkboxgroup') {
						if (fieldToFocus.getElementsByTagName("input")
								&& fieldToFocus.getElementsByTagName("input")[0]) {
							fieldToFocus.getElementsByTagName("input")[0]
									.focus();
						}
					} else
						fieldToFocus.focus();
				}

			}
		} else {
			var curObjId = obj.id;
			if (curObjId.match(/Delete__(\d)+$/)) {
				var curObjIdParts = curObjId.split("__");
				var curObjIdPartsLength = curObjIdParts.length;
				var curIndex = parseInt(curObjIdParts[curObjIdPartsLength - 1]);
				curObjIdParts[curObjIdPartsLength - 1] = ++curIndex + "";
				var fieldToFocus = this.doc.getElementById(curObjIdParts
						.join("__"));
				if (fieldToFocus) {
					fieldToFocus.focus();
				}
			}
		}
		if (e && e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		} else
			e.returnValue = false;
		return false;
	} else if (keycode == 38 && e.altKey) {
		if (field && field.tableName) {
			var curTableName = field.tableName;
			var curTable = this.getTable(curTableName);
			var curRow = obj.rowIdx;
			if (obj.name && obj.name.match(/.*Radio$/)) {
				curRow = this.doc
						.getElementById(obj.name.replace(/Radio$/, '')).rowIdx;
			}
			if (1 < curRow) {
				curRow--;
				var fieldIdToFocus = curTable.getObjectId(fieldName, curRow);
				var fieldToFocus = this.doc.getElementById(fieldIdToFocus);
				if (fieldToFocus) {
					if (obj.name && obj.name.match(/.*Radio$/)) {
						if (fieldToFocus.getElementsByTagName("input")
								&& fieldToFocus.getElementsByTagName("input")[0]) {
							fieldToFocus.getElementsByTagName("input")[0]
									.focus();
						}
					} else if (obj.className
							&& obj.className == 'checkboxgroup') {
						if (fieldToFocus.getElementsByTagName("input")
								&& fieldToFocus.getElementsByTagName("input")[0]) {
							fieldToFocus.getElementsByTagName("input")[0]
									.focus();
						}
					} else
						fieldToFocus.focus();
				}

			}
		} else {
			var curObjId = obj.id;
			if (curObjId.match(/Delete__(\d)+$/)) {
				var curObjIdParts = curObjId.split("__");
				var curObjIdPartsLength = curObjIdParts.length;
				var curIndex = parseInt(curObjIdParts[curObjIdPartsLength - 1]);
				curObjIdParts[curObjIdPartsLength - 1] = --curIndex + "";
				var fieldToFocus = this.doc.getElementById(curObjIdParts
						.join("__"));
				if (fieldToFocus) {
					fieldToFocus.focus();
				}
			}
		}
		if (e && e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		} else
			e.returnValue = false;
		return false;
	}
};

a.bulkCheckAction = function(obj, colName, tableName) {
	var table = this.tables[tableName];
	if (table.bulkCheckAction)
		table.bulkCheckAction(obj, colName);
};


text:
from django.db import models
from django.db.models import OneToOneField
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models.fields.related import SingleRelatedObjectDescriptor

# South support.
try:
    from south.modelsinspector import add_introspection_rules
    SOUTH = True
except ImportError:
    SOUTH = False

# Try to be compatible with Django 1.5+.
try:
    import json
except ImportError:
    from django.utils import simplejson as json

class AutoSingleRelatedObjectDescriptor(SingleRelatedObjectDescriptor):
    def __get__(self, instance, instance_type=None):
        try:
            return super(AutoSingleRelatedObjectDescriptor, self).__get__(instance, instance_type)
        except self.related.model.DoesNotExist:
            obj = self.related.model(**{self.related.field.name: instance})
            obj.save()
            # Don't return obj directly, otherwise it won't be added
            # to Django's cache, and the first 2 calls to obj.relobj
            # will return 2 different in-memory objects
            return super(AutoSingleRelatedObjectDescriptor, self).__get__(instance, instance_type)

class AutoOneToOneField(OneToOneField):
    '''
    OneToOneField creates related object on first call if it doesnt exist yet.
    Use it instead of original OneToOne field.

    example:

        class MyProfile(models.Model):
            user = AutoOneToOneField(User, primary_key=True)
            home_page = models.URLField(max_length=255, blank=True)
            icq = models.IntegerField(max_length=255, null=True)
    '''
    def contribute_to_related_class(self, cls, related):
        setattr(cls, related.get_accessor_name(), AutoSingleRelatedObjectDescriptor(related))

if SOUTH:
    add_introspection_rules([
        (
            (AutoOneToOneField,),
            [],
            {
                "to": ["rel.to", {}],
                "to_field": ["rel.field_name", {"default_attr": "rel.to._meta.pk.name"}],
                "related_name": ["rel.related_name", {"default": None}],
                "db_index": ["db_index", {"default": True}],
            },
        )
    ],
    ["^annoying\.fields\.AutoOneToOneField"])

class JSONField(models.TextField):
    """
    JSONField is a generic textfield that neatly serializes/unserializes
    JSON objects seamlessly.
    Django snippet #1478

    example:
        class Page(models.Model):
            data = JSONField(blank=True, null=True)

        page = Page.objects.get(pk=5)
        page.data = {'title': 'test', 'type': 3}
        page.save()
    """

    __metaclass__ = models.SubfieldBase

    def to_python(self, value):
        if value == "":
            return None

        try:
            if isinstance(value, basestring):
                return json.loads(value)
        except ValueError:
            pass
        return value

    def get_db_prep_save(self, value, *args, **kwargs):
        if value == "":
            return None
        if isinstance(value, dict) or isinstance(value, list):
            value = json.dumps(value, cls=DjangoJSONEncoder)
        return super(JSONField, self).get_db_prep_save(value, *args, **kwargs)

    def value_from_object(self, obj):
        value = super(JSONField, self).value_from_object(obj)
        if self.null and value is None:
            return None
        return json.dumps(value)

if SOUTH:
    add_introspection_rules([], ["^annoying.fields.JSONField"])


text:
package org.apache.ignite.internal.processors.metastorage.persistence;

import java.io.Serializable;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.LongStream;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteException;
import org.apache.ignite.IgniteSystemProperties;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.failure.FailureContext;
import org.apache.ignite.failure.FailureType;
import org.apache.ignite.internal.GridKernalContext;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.IgniteInterruptedCheckedException;
import org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage;
import org.apache.ignite.internal.managers.discovery.DiscoveryLocalJoinData;
import org.apache.ignite.internal.managers.discovery.GridDiscoveryManager;
import org.apache.ignite.internal.processors.GridProcessorAdapter;
import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
import org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage;
import org.apache.ignite.internal.processors.cache.persistence.metastorage.MetastorageLifecycleListener;
import org.apache.ignite.internal.processors.cache.persistence.metastorage.ReadOnlyMetastorage;
import org.apache.ignite.internal.processors.cache.persistence.metastorage.ReadWriteMetastorage;
import org.apache.ignite.internal.processors.cluster.BaselineTopology;
import org.apache.ignite.internal.processors.cluster.IgniteChangeGlobalStateSupport;
import org.apache.ignite.internal.processors.metastorage.DistributedMetaStorage;
import org.apache.ignite.internal.processors.metastorage.DistributedMetaStorageListener;
import org.apache.ignite.internal.processors.metastorage.DistributedMetastorageLifecycleListener;
import org.apache.ignite.internal.processors.subscription.GridInternalSubscriptionProcessor;
import org.apache.ignite.internal.util.future.GridFutureAdapter;
import org.apache.ignite.internal.util.typedef.internal.S;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.lang.IgniteBiTuple;
import org.apache.ignite.lang.IgniteFuture;
import org.apache.ignite.marshaller.jdk.JdkMarshaller;
import org.apache.ignite.spi.IgniteNodeValidationResult;
import org.apache.ignite.spi.discovery.DiscoveryDataBag;
import org.apache.ignite.spi.discovery.DiscoveryDataBag.GridDiscoveryData;
import org.apache.ignite.spi.discovery.DiscoveryDataBag.JoiningNodeDiscoveryData;
import org.apache.ignite.thread.IgniteThread;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

import static org.apache.ignite.IgniteSystemProperties.IGNITE_GLOBAL_METASTORAGE_HISTORY_MAX_BYTES;
import static org.apache.ignite.internal.GridComponent.DiscoveryDataExchangeType.META_STORAGE;
import static org.apache.ignite.internal.processors.cache.GridCacheUtils.isPersistenceEnabled;
import static org.apache.ignite.internal.processors.metastorage.ReadableDistributedMetaStorage.isSupported;
import static org.apache.ignite.internal.processors.metastorage.persistence.DistributedMetaStorageHistoryItem.EMPTY_ARRAY;
import static org.apache.ignite.internal.processors.metastorage.persistence.DistributedMetaStorageUtil.historyItemPrefix;
import static org.apache.ignite.internal.processors.metastorage.persistence.DistributedMetaStorageUtil.historyItemVer;
import static org.apache.ignite.internal.processors.metastorage.persistence.DistributedMetaStorageUtil.marshal;
import static org.apache.ignite.internal.processors.metastorage.persistence.DistributedMetaStorageUtil.unmarshal;
import static org.apache.ignite.internal.processors.metastorage.persistence.DistributedMetaStorageVersion.INITIAL_VERSION;

/**
 *
Implementation of {@link DistributedMetaStorage} based on {@link MetaStorage} for persistence and discovery SPI
 * for communication.
 *

 *
It is based on existing local metastorage API for persistent clusters (in-memory clusters and client nodes will
 * store data in memory). Write/remove operation use Discovery SPI to send updates to the cluster, it guarantees updates
 * order and the fact that all existing (alive) nodes have handled the update message.
 *

 *
As a way to find out which node has the latest data there is a "version" value of distributed metastorage,
 * ({@link DistributedMetaStorageVersion}) which is basically the pair {@code <number of all updates, hash of all
 * updates>}. First element of the pair is the value of {@link #getUpdatesCount()}.
 *

 *
Whole updates history until some point in the past is stored along with the data, so when an outdated node
 * connects to the cluster it will receive all the missing data and apply it locally. Listeners will also be invoked
 * after such updates. If there's not enough history stored or joining node is clear then it'll receive shapshot of
 * distributed metastorage (usually called {@code fullData} in code) so there won't be inconsistencies.
 *

 *
 * @see DistributedMetaStorageUpdateMessage
 * @see DistributedMetaStorageUpdateAckMessage
 */
public class DistributedMetaStorageImpl extends GridProcessorAdapter
    implements DistributedMetaStorage, IgniteChangeGlobalStateSupport {
    /** Component ID required for {@link DiscoveryDataBag} instances. */
    private static final int COMPONENT_ID = META_STORAGE.ordinal();

    /** Default upper bound of history size in bytes. */
    private static final long DFLT_MAX_HISTORY_BYTES = 100 * 1024 * 1024;

    /** Message indicating that clusted is in a mixed state and writing cannot be completed because of that. */
    public static final String NOT_SUPPORTED_MSG = "Ignite cluster has nodes that don't support" +
        " distributed metastorage feature. Writing cannot be completed.";

    /**
     * {@code true} if local node is client.
     */
    private final boolean isClient;

    /**
     * {@code true} if local node has persistent region in configuration and is not a client.
     */
    private final boolean isPersistenceEnabled;

    /**
     * Cached subscription processor instance. Exists to make code shorter.
     */
    private final GridInternalSubscriptionProcessor isp;

    /** */
    private volatile InMemoryCachedDistributedMetaStorageBridge bridge;

    /**
     * Version of distributed metastorage.
     */
    private volatile DistributedMetaStorageVersion ver;

    /**
     * Listeners collection. Preserves the order in which listeners were added.
     */
    final List<IgniteBiTuple<Predicate<String>, DistributedMetaStorageListener<Serializable>>> lsnrs =
        new CopyOnWriteArrayList<>();

    /**
     * All available history. Contains latest changes in distributed metastorage. Latest version is always present in
     * the cache. This means that the it is empty only if version is {@code 0}.
     */
    private final DistributedMetaStorageHistoryCache histCache = new DistributedMetaStorageHistoryCache();

    /**
     * Maximal acceptable value of {@link #histCache}'s size in bytes. History will shrink after every write until its
     * size is not greater then given value.
     */
    private final long histMaxBytes = IgniteSystemProperties.getLong(
        IGNITE_GLOBAL_METASTORAGE_HISTORY_MAX_BYTES,
        DFLT_MAX_HISTORY_BYTES
    );

    /**
     * Map with futures used to wait for async write/remove operations completion.
     */
    private final ConcurrentMap<UUID, GridFutureAdapter<Boolean>> updateFuts = new ConcurrentHashMap<>();

    /**
     * Lock to access/update data and component's state.
     */
    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    /**
     * Context marshaller.
     */
    private final JdkMarshaller marshaller;

    /**
     * Worker that will write data on disk asynchronously. Makes sence for persistent nodes only.
     */
    private final DmsDataWriterWorker worker;

    /**
     * @param ctx Kernal context.
     */
    public DistributedMetaStorageImpl(GridKernalContext ctx) {
        super(ctx);

        isClient = ctx.clientNode();

        isPersistenceEnabled = !isClient && isPersistenceEnabled(ctx.config());

        isp = ctx.internalSubscriptionProcessor();

        marshaller = ctx.marshallerContext().jdkMarshaller();

        bridge = new InMemoryCachedDistributedMetaStorageBridge(marshaller);

        //noinspection IfMayBeConditional
        if (!isPersistenceEnabled)
            worker = null;
        else {
            worker = new DmsDataWriterWorker(
                ctx.igniteInstanceName(),
                log,
                new DmsLocalMetaStorageLock() {
                    /** {@inheritDoc} */
                    @Override public void lock() {
                        localMetastorageLock();
                    }

                    /** {@inheritDoc} */
                    @Override public void unlock() {
                        localMetastorageUnlock();
                    }
                },
                this::criticalError
            );
        }

        ctx.discovery().localJoinFuture().listen(this::notifyReadyForWrite);
    }

    /**
     * {@inheritDoc}
     *

     * Create all required listeners.
     */
    @Override public void start() throws IgniteCheckedException {
        if (!isPersistenceEnabled)
            ver = INITIAL_VERSION;
        else {
            isp.registerMetastorageListener(new MetastorageLifecycleListener() {
                /** {@inheritDoc} */
                @Override public void onReadyForRead(
                    ReadOnlyMetastorage metastorage
                ) throws IgniteCheckedException {
                    onMetaStorageReadyForRead(metastorage);
                }

                /** {@inheritDoc} */
                @Override public void onReadyForReadWrite(
                    ReadWriteMetastorage metastorage
                ) {
                    onMetaStorageReadyForWrite(metastorage);
                }
            });
        }

        GridDiscoveryManager discovery = ctx.discovery();

        discovery.setCustomEventListener(
            DistributedMetaStorageUpdateMessage.class,
            this::onUpdateMessage
        );

        discovery.setCustomEventListener(
            DistributedMetaStorageUpdateAckMessage.class,
            this::onAckMessage
        );
    }

    /**
     * {@inheritDoc}
     *

     * For persistent cluster it will stop the async worker.
     *
     * @param cancel If {@code true} then ignore worker's queue and finish it as fast as possible. Otherwise
     *      just wait until queue is empty and worker completed its job.
     */
    @Override public void onKernalStop(boolean cancel) {
        lock.writeLock().lock();

        try {
            stopWorker(cancel);
        }
        finally {
            lock.writeLock().unlock();
        }
    }

    /** */
    private void stopWorker(boolean cancel) {
        assert lock.isWriteLockedByCurrentThread();

        if (isPersistenceEnabled) {
            try {
                worker.cancel(cancel);
            }
            catch (InterruptedException e) {
                log.error("Cannot stop distributed metastorage worker.", e);
            }
        }
    }

    /**
     * Executed roughly at the same time as {@link #onMetaStorageReadyForRead(ReadOnlyMetastorage)}.
     */
    public void inMemoryReadyForRead() {
        if (!isPersistenceEnabled)
            notifyReadyForRead();
    }

    /** Notify components listeners. */
    private void notifyReadyForRead() {
        for (DistributedMetastorageLifecycleListener subscriber : isp.getDistributedMetastorageSubscribers())
            subscriber.onReadyForRead(this);
    }

    /**
     * Notify components listeners.
     *
     * @param fut Local join future. There won't be any notifications for that failed it's join.
     */
    private void notifyReadyForWrite(IgniteInternalFuture<DiscoveryLocalJoinData> fut) {
        if (fut.error() == null)
            for (DistributedMetastorageLifecycleListener subscriber : isp.getDistributedMetastorageSubscribers())
                subscriber.onReadyForWrite(this);
    }

    /** {@inheritDoc} */
    @Override public void onActivate(GridKernalContext kctx) {
    }

    /**
     * {@inheritDoc}
     *

     * For persistent nodes wait until worker's queue is empty and worker completed its job.
     */
    @Override public void onDeActivate(GridKernalContext kctx) {
        if (isClient)
            return;

        lock.writeLock().lock();

        try {
            stopWorker(false);
        }
        finally {
            lock.writeLock().unlock();
        }
    }

    /**
     * Whether cluster is active at this moment or not. Also returns {@code true} if cluster is being activated.
     */
    private boolean isActive() {
        return ctx.state().clusterState().active();
    }

    /**
     * Implementation for {@link MetastorageLifecycleListener#onReadyForRead(ReadOnlyMetastorage)} listener. Invoked
     * after node was started but before it was activated (only in persistent clusters).
     *
     * @param metastorage Local metastorage instance available for reading.
     * @throws IgniteCheckedException If there were any issues while metastorage reading.
     * @see MetastorageLifecycleListener#onReadyForRead(ReadOnlyMetastorage)
     */
    private void onMetaStorageReadyForRead(ReadOnlyMetastorage metastorage) throws IgniteCheckedException {
        assert isPersistenceEnabled;

        localMetastorageLock();

        try {
            lock.writeLock().lock();

            try {
                ver = bridge.readInitialData(metastorage);

                metastorage.iterate(
                    historyItemPrefix(),
                    (key, val) -> addToHistoryCache(historyItemVer(key), (DistributedMetaStorageHistoryItem)val),
                    true
                );
            }
            finally {
                lock.writeLock().unlock();
            }
        }
        finally {
            localMetastorageUnlock();
        }

        notifyReadyForRead();
    }

    /**
     * Implementation for {@link MetastorageLifecycleListener#onReadyForReadWrite(ReadWriteMetastorage)} listener.
     * Invoked after each activation (only in persistent clusters). Restarts async worker.
     *
     * @param metastorage Local metastorage instance available for writing.
     */
    private void onMetaStorageReadyForWrite(ReadWriteMetastorage metastorage) {
        assert isPersistenceEnabled;

        worker.setMetaStorage(metastorage);

        IgniteThread workerThread = new IgniteThread(ctx.igniteInstanceName(), "dms-writer-thread", worker);

        workerThread.start();
    }

    /** {@inheritDoc} */
    @Override public long getUpdatesCount() {
        return ver.id;
    }

    /** {@inheritDoc} */
    @Override @Nullable public <T extends Serializable> T read(@NotNull String key) throws IgniteCheckedException {
        lock.readLock().lock();

        try {
            return (T)bridge.read(key);
        }
        finally {
            lock.readLock().unlock();
        }
    }

    /** {@inheritDoc} */
    @Override public void write(@NotNull String key, @NotNull Serializable val) throws IgniteCheckedException {
        assert val != null : key;

        startWrite(key, marshal(marshaller, val)).get();
    }

    /** {@inheritDoc} */
    @Override public GridFutureAdapter<?> writeAsync(
        @NotNull String key,
        @NotNull Serializable val
    ) throws IgniteCheckedException {
        assert val != null : key;

        return startWrite(key, marshal(marshaller, val));
    }

    /** {@inheritDoc} */
    @Override public GridFutureAdapter<?> removeAsync(@NotNull String key) throws IgniteCheckedException {
        return startWrite(key, null);
    }

    /** {@inheritDoc} */
    @Override public void remove(@NotNull String key) throws IgniteCheckedException {
        startWrite(key, null).get();
    }

    /** {@inheritDoc} */
    @Override public boolean compareAndSet(
        @NotNull String key,
        @Nullable Serializable expVal,
        @NotNull Serializable newVal
    ) throws IgniteCheckedException {
        assert newVal != null : key;

        return compareAndSetAsync(key, expVal, newVal).get();
    }

    /** {@inheritDoc} */
    @Override public GridFutureAdapter<Boolean> compareAndSetAsync(
        @NotNull String key,
        @Nullable Serializable expVal,
        @NotNull Serializable newVal
    ) throws IgniteCheckedException {
        assert newVal != null : key;

        return startCas(key, marshal(marshaller, expVal), marshal(marshaller, newVal));
    }

    /** {@inheritDoc} */
    @Override public boolean compareAndRemove(
        @NotNull String key,
        @NotNull Serializable expVal
    ) throws IgniteCheckedException {
        assert expVal != null : key;

        return startCas(key, marshal(marshaller, expVal), null).get();
    }

    /** {@inheritDoc} */
    @Override public void iterate(
        @NotNull String keyPrefix,
        @NotNull BiConsumer<String, ? super Serializable> cb
    ) throws IgniteCheckedException {
        lock.readLock().lock();

        try {
            bridge.iterate(keyPrefix, cb);
        }
        finally {
            lock.readLock().unlock();
        }
    }

    /** {@inheritDoc} */
    @Override public void listen(@NotNull Predicate<String> keyPred, DistributedMetaStorageListener<?> lsnr) {
        DistributedMetaStorageListener<Serializable> lsnrUnchecked = (DistributedMetaStorageListener<Serializable>)lsnr;

        lsnrs.add(new IgniteBiTuple<>(keyPred, lsnrUnchecked));
    }

    /** {@inheritDoc} */
    @Override @Nullable public DiscoveryDataExchangeType discoveryDataType() {
        return META_STORAGE;
    }

    /** {@inheritDoc} */
    @Override public void collectJoiningNodeData(DiscoveryDataBag dataBag) {
        lock.readLock().lock();

        try {
            if (isClient) {
                Serializable data = new DistributedMetaStorageJoiningNodeData(
                    getBaselineTopologyId(),
                    ver,
                    EMPTY_ARRAY
                );

                try {
                    dataBag.addJoiningNodeData(COMPONENT_ID, marshaller.marshal(data));

                    return;
                }
                catch (IgniteCheckedException e) {
                    throw new IgniteException(e);
                }
            }

            Serializable data = new DistributedMetaStorageJoiningNodeData(
                getBaselineTopologyId(),
                ver,
                histCache.toArray()
            );

            try {
                dataBag.addJoiningNodeData(COMPONENT_ID, marshaller.marshal(data));
            }
            catch (IgniteCheckedException e) {
                throw new IgniteException(e);
            }
        }
        finally {
            lock.readLock().unlock();
        }
    }

    /**
     * @return Current baseline topology id or {@code -1} if there was no baseline topology found.
     */
    private int getBaselineTopologyId() {
        BaselineTopology baselineTop = ctx.state().clusterState().baselineTopology();

        return baselineTop != null ? baselineTop.id() : -1;
    }

    /**
     * {@inheritDoc}
     *

     * If local node is client then method should do nothing. It is expected that this method is invoked on coordinator
     * node, but there might be exceptions to this. Validation rules:
     *

     *

     *         Do not join node that has no distributed metastorage if feature is supported in current topology and
     *         distributed metastorage has already been used ({@link #getUpdatesCount()} is not zero).
     *

     *

     *         Do not join node that has updates count greater then on local node and hasn't provided enough history
     *         to apply it to the cluster.
     *

     *

     *         Do not join node if its distributed metastorage version hash differs from the local one. In such cases
     *         node is probably from different cluster or has some inconsistent data.
     *

     *

     */
    @Override @Nullable public IgniteNodeValidationResult validateNode(
        ClusterNode node,
        JoiningNodeDiscoveryData discoData
    ) {
        if (isClient)
            return null;

        lock.readLock().lock();

        try {
            DistributedMetaStorageVersion locVer = ver;

            if (!discoData.hasJoiningNodeData()) {
                // Joining node doesn't support distributed metastorage feature.

                if (isSupported(ctx) && locVer.id > 0 && !(node.isClient() || node.isDaemon())) {
                    String errorMsg = "Node not supporting distributed metastorage feature" +
                        " is not allowed to join the cluster";

                    return new IgniteNodeValidationResult(node.id(), errorMsg);
                }
                else
                    return null;
            }

            DistributedMetaStorageJoiningNodeData joiningData = getJoiningNodeData(discoData);

            if (joiningData == null) {
                String errorMsg = "Cannot unmarshal joining node data";

                return new IgniteNodeValidationResult(node.id(), errorMsg);
            }

            if (!isPersistenceEnabled)
                return null;

            DistributedMetaStorageVersion remoteVer = joiningData.ver;

            DistributedMetaStorageHistoryItem[] remoteHist = joiningData.hist;

            int remoteHistSize = remoteHist.length;

            int remoteBltId = joiningData.bltId;

            boolean clusterIsActive = isActive();

            String errorMsg;

            int locBltId = getBaselineTopologyId();

            int locHistSize = histCache.size();

            if (remoteVer.id < locVer.id - locHistSize) {
                // Remote node is too far behind.
                // Technicaly this situation should be banned because there's no way to prove data consistency.
                errorMsg = null;
            }
            else if (remoteVer.id < locVer.id) {
                // Remote node it behind the cluster version and there's enough history.
                DistributedMetaStorageVersion newRemoteVer = remoteVer.nextVersion(
                    this::historyItem,
                    remoteVer.id + 1,
                    locVer.id
                );

                if (newRemoteVer.equals(locVer))
                    errorMsg = null;
                else
                    errorMsg = "Joining node has conflicting distributed metastorage data.";
            }
            else if (remoteVer.id == locVer.id) {
                // Remote and local versions match.
                if (remoteVer.equals(locVer))
                    errorMsg = null;
                else {
                    errorMsg = S.toString(
                        "Joining node has conflicting distributed metastorage data:",
                        "clusterVersion", locVer, false,
                        "joiningNodeVersion", remoteVer, false
                    );
                }
            }
            else if (remoteVer.id <= locVer.id + remoteHistSize) {
                // Remote node is ahead of the cluster and has enough history.
                if (clusterIsActive) {
                    errorMsg = "Attempting to join node with larger distributed metastorage version id." +
                        " The node is most likely in invalid state and can't be joined.";
                }
                else if (remoteBltId < locBltId)
                    errorMsg = "Joining node has conflicting distributed metastorage data.";
                else {
                    DistributedMetaStorageVersion newLocVer = locVer.nextVersion(
                        remoteHist,
                        remoteHistSize - (int)(remoteVer.id - locVer.id),
                        remoteHistSize
                    );

                    if (newLocVer.equals(remoteVer))
                        errorMsg = null;
                    else
                        errorMsg = "Joining node has conflicting distributed metastorage data.";
                }
            }
            else {
                assert remoteVer.id > locVer.id + remoteHistSize;

                // Remote node is too far ahead.
                if (clusterIsActive) {
                    errorMsg = "Attempting to join node with larger distributed metastorage version id." +
                        " The node is most likely in invalid state and can't be joined.";
                }
                else if (remoteBltId < locBltId)
                    errorMsg = "Joining node has conflicting distributed metastorage data.";
                else {
                    errorMsg = "Joining node doesn't have enough history items in distributed metastorage data." +
                        " Please check the order in which you start cluster nodes.";
                }
            }

            if (errorMsg == null)
                errorMsg = validatePayload(joiningData);

            return (errorMsg == null) ? null : new IgniteNodeValidationResult(node.id(), errorMsg);
        }
        finally {
            lock.readLock().unlock();
        }
    }

    /**
     * @param joiningData Joining data to validate.
     * @return {@code null} if contained data is valid otherwise error message.
     */
    private String validatePayload(DistributedMetaStorageJoiningNodeData joiningData) {
        for (DistributedMetaStorageHistoryItem item : joiningData.hist) {
            for (int i = 0; i < item.keys.length; i++) {
                try {
                    unmarshal(marshaller, item.valBytesArray[i]);
                }
                catch (IgniteCheckedException e) {
                    return "Unable to unmarshal key=" + item.keys[i];
                }
            }
        }

        return null;
    }

    /**
     * {@inheritDoc}
     *

     * Since {@link #validateNode(ClusterNode, DiscoveryDataBag.JoiningNodeDiscoveryData)} has already been invoked we
     * can be sure that joining node has valid discovery data. Current method does something meaningful only if joining
     * node has bigger distributed metastorage version, in this case all required updates will be applied.
     */
    @Override public void onJoiningNodeDataReceived(JoiningNodeDiscoveryData discoData) {
        if (!discoData.hasJoiningNodeData())
            return;

        DistributedMetaStorageJoiningNodeData joiningData = getJoiningNodeData(discoData);

        if (joiningData == null)
            return;

        DistributedMetaStorageVersion remoteVer = joiningData.ver;

        if (!isSupported(ctx) && remoteVer.id > 0)
            return;

        lock.writeLock().lock();

        try {
            DistributedMetaStorageVersion locVer = ver;

            if (remoteVer.id > locVer.id) {
                DistributedMetaStorageHistoryItem[] hist = joiningData.hist;

                if (remoteVer.id - locVer.id <= hist.length) {
                    for (long v = locVer.id + 1; v <= remoteVer.id; v++) {
                        int hv = (int)(v - remoteVer.id + hist.length - 1);

                        try {
                            completeWrite(hist[hv]);
                        }
                        catch (IgniteCheckedException ex) {
                            log.error("Unable to unmarshal new metastore data. update=" + hist[hv], ex);
                        }
                    }
                }
                else
                    assert false : "Joining node is too far ahead [remoteVer=" + remoteVer + "]";
            }
        }
        finally {
            lock.writeLock().unlock();
        }
    }

    /**
     * {@inheritDoc}
     *

     * Does nothing on client nodes. Also does nothinf if feature is not supported on some node in topology.
     * Otherwise it fills databag with data required for joining node so it could be consistent with the cluster.
     * There are 2 main cases: local node has enough history to send only updates or it doesn't. In first case
     * the history is collected, otherwise whole distributed metastorage ({@code fullData}) is collected along with
     * available history. Goal of collecting history in second case is to allow all nodes in cluster to have the same
     * history so connection of new server will always give the same result.
     */
    @Override public void collectGridNodeData(DiscoveryDataBag dataBag) {
        if (isClient)
            return;

        if (dataBag.commonDataCollectedFor(COMPONENT_ID))
            return;

        JoiningNodeDiscoveryData discoData = dataBag.newJoinerDiscoveryData(COMPONENT_ID);

        if (!discoData.hasJoiningNodeData())
            return;

        if (!isSupported(ctx))
            return;

        DistributedMetaStorageJoiningNodeData joiningData = getJoiningNodeData(discoData);

        if (joiningData == null)
            return;

        DistributedMetaStorageVersion remoteVer = joiningData.ver;

        lock.readLock().lock();

        try {
            DistributedMetaStorageVersion locVer = ver;

            if (remoteVer.id >= locVer.id) {
                Serializable nodeData = new DistributedMetaStorageClusterNodeData(remoteVer, null, null, null);

                dataBag.addGridCommonData(COMPONENT_ID, nodeData);
            }
            else {
                if (locVer.id - remoteVer.id <= histCache.size() && !dataBag.isJoiningNodeClient()) {
                    DistributedMetaStorageHistoryItem[] updates = history(remoteVer.id + 1, locVer.id);

                    Serializable nodeData = new DistributedMetaStorageClusterNodeData(ver, null, null, updates);

                    dataBag.addGridCommonData(COMPONENT_ID, nodeData);
                }
                else {
                    DistributedMetaStorageVersion ver0 = ver;

                    DistributedMetaStorageKeyValuePair[] fullData = bridge.localFullData();

                    DistributedMetaStorageHistoryItem[] hist;

                    if (dataBag.isJoiningNodeClient())
                        hist = EMPTY_ARRAY;
                    else
                        hist = history(ver.id - histCache.size() + 1, locVer.id);

                    Serializable nodeData = new DistributedMetaStorageClusterNodeData(ver0, fullData, hist, null);

                    dataBag.addGridCommonData(COMPONENT_ID, nodeData);
                }
            }
        }
        finally {
            lock.readLock().unlock();
        }
    }

    /**
     * Retrieve joining node data from discovery data. It is expected that it is present as a {@code byte[]} object.
     *
     * @param discoData Joining node discovery data.
     * @return Unmarshalled data or null if unmarshalling failed.
     */
    @Nullable private DistributedMetaStorageJoiningNodeData getJoiningNodeData(
        JoiningNodeDiscoveryData discoData
    ) {
        byte[] data = (byte[])discoData.joiningNodeData();

        assert data != null;

        try {
            return marshaller.unmarshal(data, U.gridClassLoader());
        }
        catch (IgniteCheckedException e) {
            log.error("Unable to unmarshal joinging node data for distributed metastorage component.", e);

            return null;
        }
    }

    /** {@inheritDoc} */
    @Override public void onDisconnected(IgniteFuture<?> reconnectFut) {
        assert isClient;

        lock.writeLock().lock();

        try {
            bridge = new InMemoryCachedDistributedMetaStorageBridge(marshaller);

            ver = INITIAL_VERSION;

            for (GridFutureAdapter<Boolean> fut : updateFuts.values())
                fut.onDone(new IgniteCheckedException("Client was disconnected during the operation."));

            updateFuts.clear();
        }
        finally {
            lock.writeLock().unlock();
        }
    }

    /** {@inheritDoc} */
    @Override public IgniteInternalFuture<?> onReconnected(boolean clusterRestarted) {
        assert isClient;

        ctx.discovery().localJoinFuture().listen(this::notifyReadyForWrite);

        return null;
    }

    /**
     * Returns last update for the specified version.
     *
     * @param specificVer Specific version.
     * @return {@code <key, value>} pair if it was found, {@code null} otherwise.
     */
    private DistributedMetaStorageHistoryItem historyItem(long specificVer) {
        return histCache.get(specificVer);
    }

    /**
     * Returns all updates in the specified range of versions.
     *
     * @param startVer Lower bound (inclusive).
     * @param actualVer Upper bound (inclusive).
     * @return Array with all requested updates sorted by version in ascending order.
     */
    private DistributedMetaStorageHistoryItem[] history(long startVer, long actualVer) {
        return LongStream.rangeClosed(startVer, actualVer)
            .mapToObj(this::historyItem)
            .toArray(DistributedMetaStorageHistoryItem[]::new);
    }

    /**
     * {@link InMemoryCachedDistributedMetaStorageBridge#localFullData()} invoked on {@link #bridge}.
     */
    @TestOnly
    private DistributedMetaStorageKeyValuePair[] localFullData() {
        return bridge.localFullData();
    }

    /**
     * {@inheritDoc}
     *

     * Applies received updates if they are present in response.
     *
     * @param data Grid discovery data.
     */
    @Override public void onGridDataReceived(GridDiscoveryData data) {
        lock.writeLock().lock();

        try {
            DistributedMetaStorageClusterNodeData nodeData = (DistributedMetaStorageClusterNodeData)data.commonData();

            if (nodeData != null) {
                if (nodeData.fullData != null) {
                    ver = nodeData.ver;

                    notifyListenersBeforeReadyForWrite(nodeData.fullData);

                    bridge.writeFullNodeData(nodeData);
                }

                if (nodeData.hist != null) {
                    clearHistoryCache();

                    for (int i = 0, len = nodeData.hist.length; i < len; i++) {
                        DistributedMetaStorageHistoryItem histItem = nodeData.hist[i];

                        addToHistoryCache(ver.id + i - (len - 1), histItem);
                    }
                }

                if (isPersistenceEnabled && nodeData.fullData != null)
                    worker.update(nodeData);

                if (nodeData.updates != null) {
                    for (DistributedMetaStorageHistoryItem update : nodeData.updates)
                        completeWrite(update);
                }
            }
            else if (!isClient && ver.id > 0) {
                throw new IgniteException("Cannot join the cluster because it doesn't support distributed metastorage" +
                    " feature and this node has not empty distributed metastorage data");
            }
        }
        catch (IgniteCheckedException ex) {
            throw new IgniteException("Cannot join the cluster", ex);
        }
        finally {
            lock.writeLock().unlock();
        }
    }

    /**
     * Common implementation for {@link #write(String, Serializable)} and {@link #remove(String)}. Synchronously waits
     * for operation to be completed.
     *
     * @param key The key.
     * @param valBytes Value bytes to write. Null if value needs to be removed.
     * @throws IgniteCheckedException If there was an error while sending discovery message.
     */
    private GridFutureAdapter<?> startWrite(String key, byte[] valBytes) throws IgniteCheckedException {
       if (!isSupported(ctx))
            throw new IgniteCheckedException(NOT_SUPPORTED_MSG);

        UUID reqId = UUID.randomUUID();

        GridFutureAdapter<Boolean> fut = new GridFutureAdapter<>();

        updateFuts.put(reqId, fut);

        DiscoveryCustomMessage msg = new DistributedMetaStorageUpdateMessage(reqId, key, valBytes);

        ctx.discovery().sendCustomEvent(msg);

        return fut;
    }

    /**
     * Basically the same as {@link #startWrite(String, byte[])} but for CAS operations.
     */
    private GridFutureAdapter<Boolean> startCas(String key, byte[] expValBytes, byte[] newValBytes)
        throws IgniteCheckedException {
         if (!isSupported(ctx))
            throw new IgniteCheckedException(NOT_SUPPORTED_MSG);

        UUID reqId = UUID.randomUUID();

        GridFutureAdapter<Boolean> fut = new GridFutureAdapter<>();

        updateFuts.put(reqId, fut);

        DiscoveryCustomMessage msg = new DistributedMetaStorageCasMessage(reqId, key, expValBytes, newValBytes);

        ctx.discovery().sendCustomEvent(msg);

        return fut;
    }

    /**
     * Invoked when {@link DistributedMetaStorageUpdateMessage} received. Attempts to store received data (depends on
     * current {@link #bridge} value). Invokes failure handler with critical error if attempt failed for some reason.
     *
     * @param topVer Ignored.
     * @param node Ignored.
     * @param msg Received message.
     */
    private void onUpdateMessage(
        AffinityTopologyVersion topVer,
        ClusterNode node,
        DistributedMetaStorageUpdateMessage msg
    ) {
        if (msg.errorMessage() != null)
            return;

        if (!isSupported(ctx)) {
            msg.errorMessage(NOT_SUPPORTED_MSG);

            return;
        }

        try {
            if (msg instanceof DistributedMetaStorageCasMessage)
                completeCas((DistributedMetaStorageCasMessage)msg);
            else
                completeWrite(new DistributedMetaStorageHistoryItem(msg.key(), msg.value()));
        }
        catch (IgniteInterruptedCheckedException e) {
            throw U.convertException(e);
        }
        catch (IgniteCheckedException | Error e) {
            throw criticalError(e);
        }
    }

    /**
     * Invoked when {@link DistributedMetaStorageUpdateAckMessage} received. Completes future if local node is the node
     * that initiated write operation.
     *
     * @param topVer Ignored.
     * @param node Ignored.
     * @param msg Received message.
     */
    private void onAckMessage(
        AffinityTopologyVersion topVer,
        ClusterNode node,
        DistributedMetaStorageUpdateAckMessage msg
    ) {
        GridFutureAdapter<Boolean> fut = updateFuts.remove(msg.requestId());

        if (fut != null) {
            String errorMsg = msg.errorMessage();

            if (errorMsg == null) {
                Boolean res = msg instanceof DistributedMetaStorageCasAckMessage
                    ? ((DistributedMetaStorageCasAckMessage)msg).updated()
                    : null;

                fut.onDone(res);
            }
            else
                fut.onDone(new IllegalStateException(errorMsg));
        }
    }

    /**
     * Invoke failure handler and rethrow passed exception, possibly wrapped into the unchecked one.
     */
    private RuntimeException criticalError(Throwable e) {
        ctx.failure().process(new FailureContext(FailureType.CRITICAL_ERROR, e));

        if (e instanceof Error)
            throw (Error)e;

        throw U.convertException((IgniteCheckedException)e);
    }

    /**
     * Store data in local metastorage or in memory.
     *
     * @param histItem {@code <key, value>} pair to process.
     * @throws IgniteCheckedException In case of IO/unmarshalling errors.
     */
    private void completeWrite(
        DistributedMetaStorageHistoryItem histItem
    ) throws IgniteCheckedException {
        lock.writeLock().lock();

        try {
            histItem = optimizeHistoryItem(histItem);

            if (histItem == null)
                return;

            ver = ver.nextVersion(histItem);

            for (int i = 0, len = histItem.keys.length; i < len; i++)
                notifyListeners(histItem.keys[i], bridge.read(histItem.keys[i]), unmarshal(marshaller, histItem.valBytesArray[i]));

            for (int i = 0, len = histItem.keys.length; i < len; i++)
                bridge.write(histItem.keys[i], histItem.valBytesArray[i]);

            addToHistoryCache(ver.id, histItem);
        }
        finally {
            lock.writeLock().unlock();
        }

        if (isPersistenceEnabled)
            worker.update(histItem);

        shrinkHistory();
    }

    /**
     * Remove updates that match already existing values.
     *
     * @param histItem New history item.
     * @return Updated history item or null is resulting history item turned out to be empty.
     */
    @Nullable private DistributedMetaStorageHistoryItem optimizeHistoryItem(
        DistributedMetaStorageHistoryItem histItem
    ) {
        String[] keys = histItem.keys;
        byte[][] valBytesArr = histItem.valBytesArray;

        int len = keys.length;
        int cnt = 0;

        BitSet matches = new BitSet(len);

        for (int i = 0; i < len; i++) {
            String key = keys[i];
            byte[] valBytes = valBytesArr[i];
            byte[] existingValBytes = bridge.readMarshalled(key);

            if (Arrays.equals(valBytes, existingValBytes))
                matches.set(i);
            else
                ++cnt;
        }

        if (cnt == 0)
            return null;

        if (cnt != len) {
            String[] newKeys = new String[cnt];
            byte[][] newValBytesArr = new byte[cnt][];

            for (int src = 0, dst = 0; src < len; src++) {
                if (!matches.get(src)) {
                    newKeys[dst] = keys[src];
                    newValBytesArr[dst] = valBytesArr[src];

                    ++dst;
                }
            }

            return new DistributedMetaStorageHistoryItem(newKeys, newValBytesArr);
        }

        return histItem;
    }

    /**
     * Store data in local metastorage or in memory.
     *
     * @param msg Message with all required data.
     * @see #completeWrite(DistributedMetaStorageHistoryItem)
     */
    private void completeCas(
        DistributedMetaStorageCasMessage msg
    ) throws IgniteCheckedException {
        if (!msg.matches())
            return;

        Serializable oldVal = bridge.read(msg.key());

        Serializable expVal = unmarshal(marshaller, msg.expectedValue());

        if (!Objects.deepEquals(oldVal, expVal)) {
            msg.setMatches(false);

            // Do nothing if expected value doesn't match with the actual one.
            return;
        }

        completeWrite(new DistributedMetaStorageHistoryItem(msg.key(), msg.value()));
    }

    /**
     * Store current update into the in-memory history cache.
     *
     * @param ver Version for the update.
     * @param histItem Update itself.
     */
    void addToHistoryCache(long ver, DistributedMetaStorageHistoryItem histItem) {
        assert lock.isWriteLockedByCurrentThread();

        if (!isClient)
            histCache.put(ver, histItem);
    }

    /**
     * Clear in-memory history cache.
     */
    void clearHistoryCache() {
        assert lock.isWriteLockedByCurrentThread();

        histCache.clear();
    }

    /**
     * Shrikn history so that its estimating size doesn't exceed {@link #histMaxBytes}.
     */
    private void shrinkHistory() {
        lock.writeLock().lock();

        try {
            while (histCache.sizeInBytes() > histMaxBytes && histCache.size() > 1) {
                histCache.removeOldest();

                if (isPersistenceEnabled)
                    worker.removeHistItem(ver.id - histCache.size());
            }
        }
        finally {
            lock.writeLock().unlock();
        }
    }

    /**
     * Notify listeners on node start. Even if there was no data restoring.
     *
     * @param newData Data about which listeners should be notified.
     */
    private void notifyListenersBeforeReadyForWrite(
        DistributedMetaStorageKeyValuePair[] newData
    ) throws IgniteCheckedException {
        assert lock.isWriteLockedByCurrentThread();

        DistributedMetaStorageKeyValuePair[] oldData = bridge.localFullData();

        int oldIdx = 0, newIdx = 0;

        while (oldIdx < oldData.length && newIdx < newData.length) {
            String oldKey = oldData[oldIdx].key;
            byte[] oldValBytes = oldData[oldIdx].valBytes;

            String newKey = newData[newIdx].key;
            byte[] newValBytes = newData[newIdx].valBytes;

            int c = oldKey.compareTo(newKey);

            if (c < 0) {
                notifyListeners(oldKey, unmarshal(marshaller, oldValBytes), null);

                ++oldIdx;
            }
            else if (c > 0) {
                notifyListeners(newKey, null, unmarshal(marshaller, newValBytes));

                ++newIdx;
            }
            else {
                Serializable oldVal = unmarshal(marshaller, oldValBytes);

                Serializable newVal = Arrays.equals(oldValBytes, newValBytes) ? oldVal : unmarshal(marshaller, newValBytes);

                notifyListeners(oldKey, oldVal, newVal);

                ++oldIdx;

                ++newIdx;
            }
        }

        for (; oldIdx < oldData.length; ++oldIdx)
            notifyListeners(oldData[oldIdx].key, unmarshal(marshaller, oldData[oldIdx].valBytes), null);

        for (; newIdx < newData.length; ++newIdx)
            notifyListeners(newData[newIdx].key, null, unmarshal(marshaller, newData[newIdx].valBytes));
    }

    /**
     * Notify listeners.
     *
     * @param key The key.
     * @param oldVal Old value.
     * @param newVal New value.
     */
    private void notifyListeners(String key, Serializable oldVal, Serializable newVal) {
        for (IgniteBiTuple<Predicate<String>, DistributedMetaStorageListener<Serializable>> entry : lsnrs) {
            if (entry.get1().test(key)) {
                try {
                    // ClassCastException might be thrown here for crappy listeners.
                    entry.get2().onUpdate(key, oldVal, newVal);
                }
                catch (Exception e) {
                    log.error(S.toString(
                        "Failed to notify distributed metastorage update listener",
                        "key", key, false,
                        "oldVal", oldVal, false,
                        "newVal", newVal, false,
                        "lsnr", entry.get2(), false
                    ), e);
                }
            }
        }
    }

    /** Checkpoint read lock. */
    private void localMetastorageLock() {
        ctx.cache().context().database().checkpointReadLock();
    }

    /** Checkpoint read unlock. */
    private void localMetastorageUnlock() {
        ctx.cache().context().database().checkpointReadUnlock();
    }
}


text:
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.Azure.Graph.RBAC;
using Microsoft.Azure.Graph.RBAC.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using ProjectResources = Microsoft.Azure.Commands.Resources.Properties.Resources;

namespace Microsoft.Azure.Commands.Resources.Models.ActiveDirectory
{
    public class ActiveDirectoryClient
    {
        public GraphRbacManagementClient GraphClient { get; private set; }

        /// <summary>
        /// Creates new ActiveDirectoryClient using WindowsAzureSubscription.
        /// </summary>
        /// <param name="context"></param>
        public ActiveDirectoryClient(AzureContext context)
        {
            AccessTokenCredential creds = (AccessTokenCredential)AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(context);
            GraphClient = AzureSession.ClientFactory.CreateCustomClient<GraphRbacManagementClient>(
                creds.TenantID,
                creds,
                context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.Graph));
        }

        public PSADObject GetADObject(ADObjectFilterOptions options)
        {
            PSADObject result = null;

            Debug.Assert(options != null);

            if (IsSet(options.SignInName, options.Mail, options.UPN, options.Id))
            {
                result = FilterUsers(options).FirstOrDefault();
            }

            if (result == null && IsSet(options.SPN, options.Id))
            {
                result = FilterServicePrincipals(options).FirstOrDefault();
            }

            if (result == null && IsSet(options.Mail, options.Id))
            {
                result = FilterGroups(options).FirstOrDefault();
            }

            return result;
        }

        private static bool IsSet(params string[] strings)
        {
            return strings.Any(s => !string.IsNullOrEmpty(s));
        }

        private static string Normalize(string s)
        {
            return string.IsNullOrEmpty(s) ? null : s;
        }

        public List<PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterOptions options)
        {
            List<PSADServicePrincipal> servicePrincipals = new List<PSADServicePrincipal>();
            ServicePrincipalListResult result = new ServicePrincipalListResult();
            ServicePrincipal servicePrincipal = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipal.Get(options.Id).ServicePrincipal;
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else if (!string.IsNullOrEmpty(options.SPN))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipal.GetByServicePrincipalName(options.SPN).ServicePrincipals.FirstOrDefault();
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.ServicePrincipal.List(options.SearchString);
                    }
                    else
                    {
                        result = GraphClient.ServicePrincipal.ListNext(options.NextLink);
                    }

                    servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.ServicePrincipal.List(options.SearchString);
                    servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.ServicePrincipal.ListNext(result.NextLink);
                        servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal()));
                    }
                }
            }

            return servicePrincipals;
        }

        public List<PSADServicePrincipal> FilterServices()
        {
            return FilterServicePrincipals(new ADObjectFilterOptions());
        }

        public List<PSADUser> FilterUsers(ADObjectFilterOptions options)
        {
            List<PSADUser> users = new List<PSADUser>();
            UserListResult result = new UserListResult();
            User user = null;

            if (!string.IsNullOrEmpty(options.Id) || !string.IsNullOrEmpty(options.UPN))
            {
                try
                {
                    user = GraphClient.User.Get(Normalize(options.Id) ?? Normalize(options.UPN)).User;
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    users.Add(user.ToPSADUser());
                }
            }
            else if (!string.IsNullOrEmpty(options.Mail) || !string.IsNullOrEmpty(options.SignInName))
            {
                try
                {
                    user = GraphClient.User.GetBySignInName(Normalize(options.Mail) ?? Normalize(options.SignInName)).Users.FirstOrDefault();
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    users.Add(user.ToPSADUser());
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.User.List(null, options.SearchString);
                    }
                    else
                    {
                        result = GraphClient.User.ListNext(options.NextLink);
                    }

                    users.AddRange(result.Users.Select(u => u.ToPSADUser()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.User.List(null, options.SearchString);
                    users.AddRange(result.Users.Select(u => u.ToPSADUser()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.User.ListNext(result.NextLink);
                        users.AddRange(result.Users.Select(u => u.ToPSADUser()));
                    }
                }
            }

            return users;
        }

        public List<PSADUser> FilterUsers()
        {
            return FilterUsers(new ADObjectFilterOptions());
        }

        public List<PSADObject> ListUserGroups(string principal)
        {
            List<PSADObject> result = new List<PSADObject>();
            Guid objectId = GetObjectId(new ADObjectFilterOptions { UPN = principal });
            PSADObject user = GetADObject(new ADObjectFilterOptions { Id = objectId.ToString() });
            var groupsIds = GraphClient.User.GetMemberGroups(new UserGetMemberGroupsParameters { ObjectId = user.Id.ToString() }).ObjectIds;
            var groupsResult = GraphClient.Objects.GetObjectsByObjectIds(new GetObjectsParameters { Ids = groupsIds });
            result.AddRange(groupsResult.AADObject.Select(g => g.ToPSADGroup()));

            return result;
        }

        public List<PSADObject> GetObjectsByObjectId(List<string> objectIds)
        {
            List<PSADObject> result = new List<PSADObject>();
            var adObjects = GraphClient.Objects.GetObjectsByObjectIds(new GetObjectsParameters { Ids = objectIds, IncludeDirectoryObjectReferences = true }).AADObject;
            result.AddRange(adObjects.Select(o => o.ToPSADObject()));
            return result;
        }

        public List<PSADGroup> FilterGroups(ADObjectFilterOptions options)
        {
            List<PSADGroup> groups = new List<PSADGroup>();
            Group group = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    group = GraphClient.Group.Get(options.Id).Group;
                }
                catch {  /* The group does not exist, ignore the exception */ }

                if (group != null)
                {
                    groups.Add(group.ToPSADGroup());
                }
            }
            else
            {
                GroupListResult result = new GroupListResult();

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.Group.List(options.Mail, options.SearchString);
                    }
                    else
                    {
                        result = GraphClient.Group.ListNext(options.NextLink);
                    }

                    groups.AddRange(result.Groups.Select(g => g.ToPSADGroup()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.Group.List(options.Mail, options.SearchString);
                    groups.AddRange(result.Groups.Select(g => g.ToPSADGroup()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.Group.ListNext(result.NextLink);
                        groups.AddRange(result.Groups.Select(g => g.ToPSADGroup()));
                    }
                }
            }

            return groups;
        }

        public List<PSADGroup> FilterGroups()
        {
            return FilterGroups(new ADObjectFilterOptions());
        }

        public List<PSADObject> GetGroupMembers(ADObjectFilterOptions options)
        {
            List<PSADObject> members = new List<PSADObject>();
            PSADObject group = FilterGroups(options).FirstOrDefault();

            if (group != null)
            {
                GetObjectsResult result = new GetObjectsResult();

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.Group.GetGroupMembers(group.Id.ToString());
                    }
                    else
                    {
                        result = GraphClient.Group.GetGroupMembersNext(result.NextLink);
                    }

                    members.AddRange(result.AADObject.Select(u => u.ToPSADObject()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.Group.GetGroupMembers(group.Id.ToString());
                    members.AddRange(result.AADObject.Select(u => u.ToPSADObject()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.Group.GetGroupMembersNext(result.NextLink);
                        members.AddRange(result.AADObject.Select(u => u.ToPSADObject()));
                    }
                }
            }

            return members;
        }

        public Guid GetObjectId(ADObjectFilterOptions options)
        {
            Guid principalId;
            if (options != null && options.Id != null
                && Guid.TryParse(options.Id, out principalId))
            {
                // do nothing, we have parsed the guid
            }
            else
            {
                PSADObject adObj = GetADObject(options);

                if (adObj == null)
                {
                    throw new KeyNotFoundException("The provided information does not map to an AD object id.");
                }

                principalId = adObj.Id;
            }

            return principalId;
        }

        public PSADApplication CreateApplication(CreatePSApplicationParameters createParameters)
        {
            IList<PasswordCredential> passwordCredentials = createParameters.PasswordCredentials != null
                ? createParameters.PasswordCredentials.Select(psCredential => psCredential.ToGraphPasswordCredential()).ToList()
                : null;

            IList<KeyCredential> keyCredentials = createParameters.KeyCredentials != null
                ? createParameters.KeyCredentials.Select(psCredential => psCredential.ToGraphKeyCredential()).ToList()
                : null;

            ApplicationCreateParameters graphParameters = new ApplicationCreateParameters
            {
                DisplayName = createParameters.DisplayName,
                Homepage = createParameters.HomePage,
                IdentifierUris = createParameters.IdentifierUris,
                PasswordCredentials = passwordCredentials,
                KeyCredentials = keyCredentials
            };

            return GraphClient.Application.Create(graphParameters).Application.ToPSADApplication();
        }

        public void RemoveApplication(string applicationObjectId)
        {
            GraphClient.Application.Delete(applicationObjectId.ToString());
        }

        public PSADServicePrincipal CreateServicePrincipal(CreatePSServicePrincipalParameters createParameters)
        {
            ServicePrincipalCreateParameters graphParameters = new ServicePrincipalCreateParameters
            {
                AppId = createParameters.ApplicationId.ToString(),
                AccountEnabled = createParameters.AccountEnabled
            };

            return GraphClient.ServicePrincipal.Create(graphParameters).ServicePrincipal.ToPSADServicePrincipal();
        }

        public PSADServicePrincipal RemoveServicePrincipal(string objectId)
        {
            ADObjectFilterOptions options = new ADObjectFilterOptions
            {
                Id = objectId.ToString()
            };

            PSADServicePrincipal servicePrincipal = FilterServicePrincipals(options).FirstOrDefault();
            if (servicePrincipal != null)
            {
                GraphClient.ServicePrincipal.Delete(objectId);
            }
            else
            {
                throw new KeyNotFoundException(string.Format(ProjectResources.ServicePrincipalDoesntExist, objectId));
            }

            return servicePrincipal;
        }
    }
}


text:
namespace leveldb {

const char* Status::CopyState(const char* state) {
  uint32_t size;
  memcpy(&size, state, sizeof(size));
  char* result = new char[size + 5];
  memcpy(result, state, size + 5);
  return result;
}

Status::Status(Code code, const Slice& msg, const Slice& msg2) {
  assert(code != kOk);
  const uint32_t len1 = msg.size();
  const uint32_t len2 = msg2.size();
  const uint32_t size = len1 + (len2 ? (2 + len2) : 0);
  char* result = new char[size + 5];
  memcpy(result, &size, sizeof(size));
  result[4] = static_cast<char>(code);
  memcpy(result + 5, msg.data(), len1);
  if (len2) {
    result[5 + len1] = ':';
    result[6 + len1] = ' ';
    memcpy(result + 7 + len1, msg2.data(), len2);
  }
  state_ = result;
}

std::string Status::ToString() const {
  if (state_ == NULL) {
    return "OK";
  } else {
    char tmp[30];
    const char* type;
    switch (code()) {
      case kOk:
        type = "OK";
        break;
      case kNotFound:
        type = "NotFound: ";
        break;
      case kCorruption:
        type = "Corruption: ";
        break;
      case kNotSupported:
        type = "Not implemented: ";
        break;
      case kInvalidArgument:
        type = "Invalid argument: ";
        break;
      case kIOError:
        type = "IO error: ";
        break;
      case kTimeOut:
        type = "Timeout error: ";
        break;
      case kIOPermissionDenied:
        type = "IO Permission Denied: ";
        break;
      case kReject:
        type = "Reject: ";
        break;
      default:
        snprintf(tmp, sizeof(tmp), "Unknown code(%d): ", static_cast<int>(code()));
        type = tmp;
        break;
    }
    std::string result(type);
    uint32_t length;
    memcpy(&length, state_, sizeof(length));
    result.append(state_ + 5, length);
    return result;
  }
}

}  // namespace leveldb


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
null

#### Original name
Phoma helicis Pass. & Beltr.

### Remarks
null


text:
#ifndef NX_FLUIDS_NXPARTICLEDATA
#define NX_FLUIDS_NXPARTICLEDATA
/** \addtogroup fluids
  @{
*/

/*----------------------------------------------------------------------------*\
|
|					Public Interface to NVIDIA PhysX Technology
|
|							     www.nvidia.com
|
\*----------------------------------------------------------------------------*/

/**
Particle flags are used to give some additional information about the particles.
*/
enum NxParticleFlag
{
	NX_FP_COLLISION_WITH_STATIC		= (1<<0),
	NX_FP_COLLISION_WITH_DYNAMIC	= (1<<1),
	NX_FP_SEPARATED					= (1<<2),
	NX_FP_MOTION_LIMIT_REACHED		= (1<<3),
};

/**
\brief Descriptor-like user-side class describing a set of fluid particles.

NxParticleData is used to submit particles to the simulation and
to retrieve information about particles in the simulation.

Each particle is described by its position, velocity, lifetime, density, and a set of (NxParticleFlag) flags.
The memory for the particle data is allocated by the application,
making this class a "user buffer wrapper".
*/
class NxParticleData
	{
	public:

	/**
	\brief Points to the user-allocated memory holding the number of elements stored in the buffers.

	If the SDK writes to a given particle buffer, it also sets the numbers of elements written. If
	numParticlesPtr is set to NULL, the SDK can't write to the given buffer.
	When passing particles to the SDK, the number stored at the location is used to determine
	how many particles need to be read from the buffer. Therefore it is critical to set the value
	numParticlesPtr is pointing to, to the correct number of particles which are prepared to be added
	to the simulation. Otherwise erroneous data gets processed which has undefined results.
	*/
	NxU32*					numParticlesPtr;

	/**
	\brief The pointer to the user-allocated buffer for particle positions.

	A position consists of three consecutive 32-bit floats. If set to NULL, positions are not read or written to.
	*/
	NxF32*					bufferPos;

	/**
	\brief The pointer to the user-allocated buffer for particle velocities.

	A velocity consists of three consecutive 32-bit
	floats. If set to NULL, velocities are not read or written to.
	*/
	NxF32*					bufferVel;

	/**
	\brief The pointer to the user-allocated buffer for particle lifetimes.

	A particle lifetime consists of one 32-bit
	float. If set to NULL, lifetimes are not read or written to.
	*/
	NxF32*					bufferLife;

	/**
	\brief The pointer to the user-allocated buffer for particle densities.

	A particle density consists of one 32-bit float. If set to NULL, densities are not written to.
	Densities are never read from the user, they are always defined by the fluid simulation.
	*/
	NxF32*					bufferDensity;

	/**
	\brief The pointer to the user-allocated buffer for particle IDs.

	A particle id is represented as a 32-bit unsigned integer. If set to NULL, IDs are not written to.
	IDs are never read from the user, they are always defined by the SDK.
	*/
	NxU32*					bufferId;

	/**
	\brief The pointer to the user-allocated buffer for particle flags.

	A particle flags are represented as a 32-bit unsigned integer. If set to NULL, flags are not written to.
	Flags are never read from the user, they are always defined by the SDK.
	Use NxParticleFlag to interpret this data.
	*/
	NxU32*					bufferFlag;

	/**
	\brief The pointer to the user-allocated buffer for particle collision normals.

	A collision normal consists of three consecutive 32-bit
	floats. If set to NULL, normals are not read or written to.

	Limitation: Collision normals can only be received if
	NxFluidDesc.flags.NX_FF_COLLISION_TWOWAY is NOT set.
	*/
	NxF32*					bufferCollisionNormal;

	/**
	\brief The separation (in bytes) between consecutive particle positions.

	The position of the first particle is found at location <tt>bufferPos</tt>;
	the second is at <tt>bufferPos + bufferPosByteStride</tt>;
	and so on.
	*/
	NxU32					bufferPosByteStride;

	/**
	\brief The separation (in bytes) between consecutive particle velocities.

	The velocity of the first particle is found at location <tt>bufferVel</tt>;
	the second is at <tt>bufferVel + bufferVelByteStride</tt>;
	and so on.
	*/
	NxU32					bufferVelByteStride;

	/**
	\brief The separation (in bytes) between consecutive particle lifetimes.

	The lifetime of the first particle is found at location <tt>bufferLife</tt>;
	the second is at <tt>bufferLife + bufferLifeByteStride</tt>;
	and so on.
	*/
	NxU32					bufferLifeByteStride;

	/**
	\brief The separation (in bytes) between consecutive particle densities.

	The density of the first particle is found at location <tt>bufferDensity</tt>;
	the second is at <tt>bufferDensity + bufferDensityByteStride</tt>;
	and so on.
	*/
	NxU32					bufferDensityByteStride;

	/**
	\brief The separation (in bytes) between consecutive particle IDs.

	The ID of the first particle is found at location <tt>bufferId</tt>;
	the second is at <tt>bufferId + bufferIdByteStride</tt>;
	and so on.
	*/
	NxU32					bufferIdByteStride;

	/**
	\brief The separation (in bytes) between consecutive particle flags.

	The flags of the first particle is found at location <tt>bufferFlag</tt>;
	the second is at <tt>bufferFlag + bufferFlagByteStride</tt>;
	and so on.
	*/
	NxU32					bufferFlagByteStride;

	/**
	\brief The separation (in bytes) between consecutive particle collision normals.

	The normal of the first particle is found at location <tt>bufferCollisionNormal</tt>;
	the second is at <tt>bufferCollisionNormal + bufferCollisionNormalByteStride</tt>;
	and so on.
	*/
	NxU32					bufferCollisionNormalByteStride;

	const char*				name;			//!< Possible debug name. The string is not copied by the SDK, only the pointer is stored.

	NX_INLINE ~NxParticleData();
	/**
	\brief (Re)sets the structure to the default.
	*/
	NX_INLINE void setToDefault();
	/**
	\brief Returns true if the current settings are valid
	*/
	NX_INLINE bool isValid() const;

	/**
	\brief Constructor sets to default.
	*/
	NX_INLINE	NxParticleData();
	};

NX_INLINE NxParticleData::NxParticleData()
	{
	setToDefault();
	}

NX_INLINE NxParticleData::~NxParticleData()
	{
	}

NX_INLINE void NxParticleData::setToDefault()
	{
	numParticlesPtr						= NULL;
	bufferPos							= NULL;
	bufferVel							= NULL;
	bufferLife							= NULL;
	bufferDensity						= NULL;
	bufferId							= NULL;
	bufferFlag							= NULL;
	bufferCollisionNormal				= NULL;
	bufferPosByteStride					= 0;
	bufferVelByteStride					= 0;
	bufferLifeByteStride				= 0;
	bufferDensityByteStride				= 0;
	bufferIdByteStride					= 0;
	bufferFlagByteStride				= 0;
	bufferCollisionNormalByteStride		= 0;
	name								= NULL;
	}

NX_INLINE bool NxParticleData::isValid() const
	{
	if (numParticlesPtr && !(bufferPos || bufferVel || bufferLife || bufferDensity || bufferId || bufferCollisionNormal)) return false;
	if ((bufferPos || bufferVel || bufferLife || bufferDensity || bufferId || bufferCollisionNormal) && !numParticlesPtr) return false;
	if (bufferPos && !bufferPosByteStride) return false;
	if (bufferVel && !bufferVelByteStride) return false;
	if (bufferLife && !bufferLifeByteStride) return false;
	if (bufferDensity && !bufferDensityByteStride) return false;
	if (bufferId && !bufferIdByteStride) return false;
    if (bufferFlag && !bufferFlagByteStride) return false;
	if (bufferCollisionNormal && !bufferCollisionNormalByteStride) return false;
	return true;
	}

/** @} */
#endif

//NVIDIACOPYRIGHTBEGIN
///////////////////////////////////////////////////////////////////////////
// Copyright (c) 2010 NVIDIA Corporation
// All rights reserved. www.nvidia.com
///////////////////////////////////////////////////////////////////////////
//NVIDIACOPYRIGHTEND