private void Init() { this.GenericBytesComparer = new Kooboo.IndexedDB.Btree.Comparer.EqualityComparer(this.StoreSetting.PrimaryKeyLen); this.TrySetVersionFunction(); Type keytype = typeof(TKey); Helper.IndexHelper.VerifyIndexType(keytype); this.IsStringKey = keytype == typeof(string); // StoreVersionUpgrade.Upgrade(this); if (KeyConverter != null && ValueConverter != null && this.primaryIndex != null && this.Indexes != null) { return; } if (KeyConverter == null) { KeyConverter = ObjectContainer.GetConverter <TKey>(); } if (ValueConverter == null) { if (this.StoreSetting.UseDefaultNETBinaryFormater) { ValueConverter = ObjectContainer.GetBinaryConverter <TValue>(); } else { ValueConverter = ObjectContainer.GetConverter <TValue>(this.SettingColumns); } } this.primaryIndex = new Btree.BtreeIndex <TKey>(this.StoreSetting.PrimaryKey, true, this.StoreSetting.PrimaryKeyLen, Helper.IndexHelper.GetIndexFileName(this.ObjectFolder, this.StoreSetting.PrimaryKey), this.StoreSetting.MaxCacheLevel); IndexInstanceList <TValue> indexClassInstance = new IndexInstanceList <TValue>(); indexClassInstance.ParseSetting(this.StoreSetting.Indexs, this.ObjectFolder, this.StoreSetting.MaxCacheLevel); this.Indexes = indexClassInstance; if (indexClassInstance.items.Count > 0) { this.HasIndex = true; } }
/// <summary> /// Create an additional index. The primary key index will be automatically created. /// When create a new index, it will trigger a rebuild of that index. /// </summary> /// <param name="fieldName"></param> /// <param name="unique"></param> /// <param name="maxkeylen">For string type of index, you can define a </param> private void CreateIndex(string fieldName, bool unique, int maxkeylen = 0) { lock (_Locker) { if (this.Indexes.HasIndex(fieldName)) { throw new Exception("the index " + fieldName + " already exits"); } Type keytype = Helper.TypeHelper.GetFieldType <TValue>(fieldName); Helper.IndexHelper.VerifyIndexType(keytype); maxkeylen = Helper.KeyHelper.GetKeyLen(keytype, maxkeylen); var indexinstance = IndexInstanceList <TValue> .GetIndexInstance(ObjectFolder, fieldName, maxkeylen, this.StoreSetting.MaxCacheLevel); try { foreach (var item in this.primaryIndex.allItemCollection(true)) { var value = this.getValue(item); indexinstance.Add(value, item); } } catch (Exception) { indexinstance.Close(); indexinstance.DelSelf(); throw new Exception("build index failed, can be uniqueness contraints or others."); } this.Indexes.items.Add(indexinstance); this.StoreSetting.Indexs[fieldName] = maxkeylen; Helper.SettingHelper.UpdateSetting(this.StoreSettingFile, this.StoreSetting); } }