Beispiel #1
0
        private Database CreateNewDatabase <TKey, TEntity>(bool autoGenerateKey)
        {
            var keyType    = typeof(TKey);
            var name       = GetEntityName <TEntity>();
            var indexId    = Guid.NewGuid();
            var databaseId = Guid.NewGuid();
            var database   = IntialiseDatabase(keyType, indexId, databaseId);

            _databases[name] = database;
            _addressSpaceIndex.MetaData.Add(new DatabaseMetaData
            {
                DatabaseName = name,
                IndexId      = indexId,
                DataId       = databaseId,
                KeyType      = keyType
            });

            if (autoGenerateKey)
            {
                var typeComparer = TypeComparer <TKey> .GetTypeComparer();

                var autoGenIndexKey = typeComparer.Increment(typeComparer.MinKey);

                var autoGen = new AutoGenIdentity <TKey>()
                {
                    CurrentUsedId = autoGenIndexKey, EntityTypeName = name
                };
                _keyGenerators[typeof(TEntity)].RegisterAutoGenIdentity(autoGen);
                autoGen.NextRetrieved += OnAutoGenIdentityRetrievedNextValue;
            }
            _addressSpaceManager.ResetAddressSpace(AddressSpaceIndex.IndexRootId);
            _addressSpaceIndex.SaveToDisk();
            return(database);
        }
Beispiel #2
0
        void OnAutoGenIdentityRetrievedNextValue <TKey>(AutoGenIdentity <TKey> sender)
        {
            var typeComparer = TypeComparer <TKey> .GetTypeComparer();

            var autoGenIndexKey  = typeComparer.Increment(typeComparer.MinKey);
            var databaseToUpdate = _databases[sender.EntityTypeName];
            var dataText         = JsonConvert.SerializeObject(sender);

            databaseToUpdate.Write(DataKey.CreateKey <TKey>(autoGenIndexKey), dataText);
        }
Beispiel #3
0
        private AutoGenIdentity <TKey> RetrieveAutoKeyState <TKey, TEntity>()
        {
            AutoGenIdentity <TKey> result;
            var database     = _databases[GetEntityName <TEntity>()];
            var typeComparer = TypeComparer <TKey> .GetTypeComparer();

            var autoGenIndexKey = typeComparer.Increment(typeComparer.MinKey);
            var text            = database.Read(DataKey.CreateKey(autoGenIndexKey));

            if (!string.IsNullOrEmpty(text))
            {
                var record = new JellyRecord <AutoGenIdentity <TKey> >(text);
                result = record.Entity;
                result.NextRetrieved += OnAutoGenIdentityRetrievedNextValue;
                return(result);
            }
            result = new AutoGenIdentity <TKey>()
            {
                CurrentUsedId = autoGenIndexKey, EntityTypeName = GetEntityName <TEntity>()
            };
            result.NextRetrieved += OnAutoGenIdentityRetrievedNextValue;
            return(result);
        }