/// <summary> /// Add a new collection. Check if name the not exists /// </summary> public CollectionPage Add(string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } if (!CollectionPage.NamePattern.IsMatch(name)) { throw LiteException.InvalidFormat(name); } _log.Write(Logger.COMMAND, "creating new collection '{0}'", name); // get header marked as dirty because I will use header after (and NewPage can get another header instance) var header = _pager.GetPage <HeaderPage>(0); // check limit count (8 bytes per collection = 4 to string length, 4 for uint pageID) if (header.CollectionPages.Sum(x => x.Key.Length + 8) + name.Length + 8 >= CollectionPage.MAX_COLLECTIONS_SIZE) { throw LiteException.CollectionLimitExceeded(CollectionPage.MAX_COLLECTIONS_SIZE); } // get new collection page (marked as dirty) var col = _pager.NewPage <CollectionPage>(); // add this page to header page collection header.CollectionPages.Add(name, col.PageID); col.CollectionName = name; // set header page as dirty _pager.SetDirty(header); // create PK index var pk = _indexer.CreateIndex(col); pk.Field = "_id"; pk.Expression = "$._id"; pk.Unique = true; return(col); }
/// <summary> /// Add a new collection. Check if name the not exists /// </summary> public CollectionPage Add(string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (!CollectionPage.NamePattern.IsMatch(name)) { throw LiteException.InvalidFormat("CollectionName", name); } // get header marked as dirty because I will use header after (and NewPage can get another header instance) var header = _pager.GetPage <HeaderPage>(0, true); // check limit count if (header.CollectionPages.Count >= CollectionPage.MAX_COLLECTIONS) { throw LiteException.CollectionLimitExceeded(CollectionPage.MAX_COLLECTIONS); } // get new collection page (marked as dirty) var col = _pager.NewPage <CollectionPage>(); // add this page to header page collection header.CollectionPages.Add(name.Trim().ToLower(), col.PageID); col.CollectionName = name; // create PK index var pk = _indexer.CreateIndex(col); pk.Field = "_id"; pk.Options = new IndexOptions { Unique = true }; return(col); }
/// <summary> /// Add a new collection. Check if name the not exists /// </summary> public CollectionPage Add(string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (!CollectionPage.NamePattern.IsMatch(name)) { throw LiteException.InvalidFormat("CollectionName", name); } // test collection limit var pages = _pager.GetSeqPages <CollectionPage>(_cache.Header.FirstCollectionPageID); if (pages.Count() >= CollectionPage.MAX_COLLECTIONS) { throw LiteException.CollectionLimitExceeded(CollectionPage.MAX_COLLECTIONS); } var col = _pager.NewPage <CollectionPage>(); // add page in collection list _pager.AddOrRemoveToFreeList(true, col, _cache.Header, ref _cache.Header.FirstCollectionPageID); col.CollectionName = name; col.IsDirty = true; // create PK index var pk = _indexer.CreateIndex(col); pk.Field = "_id"; pk.Options = new IndexOptions { Unique = true }; return(col); }