DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SnapshotIndexSet.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
20 
21 namespace Deveel.Data.Index {
22  internal class SnapshotIndexSet : IIndexSet {
23  private readonly IndexSetStore indexSetStore;
24  private List<StoreIndex> indexes;
25 
26  private bool disposed;
27 
28  private static readonly StoreIndex[] EmptyIndex = new StoreIndex[0];
29 
30  public SnapshotIndexSet(IndexSetStore indexSetStore, IndexBlock[] blocks) {
31  this.indexSetStore = indexSetStore;
32  IndexBlocks = blocks;
33 
34  // Not disposed.
35  disposed = false;
36 
37  }
38 
40  Dispose(false);
41  }
42 
43  public IEnumerable<StoreIndex> AllIndices {
44  get {
45  if (indexes == null)
46  return EmptyIndex;
47 
48  return indexes.ToArray();
49  }
50  }
51 
52  public IndexBlock[] IndexBlocks { get; private set; }
53 
54  public void Dispose() {
55  Dispose(true);
56  GC.SuppressFinalize(this);
57  }
58 
59  private void Dispose(bool disposing) {
60  if (!disposed) {
61  if (disposing) {
62  try {
63  if (indexes != null) {
64  foreach (var index in indexes) {
65  index.Dispose();
66  }
67  }
68 
69  // Release reference to the index_blocks;
70  foreach (var block in IndexBlocks) {
71  block.RemoveReference();
72  }
73  } catch (Exception) {
74  }
75  }
76 
77  indexes = null;
78  IndexBlocks = null;
79  disposed = true;
80  }
81  }
82 
83 
84  public IIndex GetIndex(int offset) {
85  // Create if not exist.
86  if (indexes == null) {
87  indexes = new List<StoreIndex>();
88  } else {
89  // If this list has already been created, return it
90  foreach (var index in indexes) {
91  if (index.IndexNumber == offset)
92  return index;
93  }
94  }
95 
96  try {
97  var index = (StoreIndex) IndexBlocks[offset].CreateIndex();
98  indexes.Add(index);
99  return index;
100  } catch (IOException e) {
101  throw new Exception("IO Error: " + e.Message, e);
102  }
103  }
104  }
105 }
SnapshotIndexSet(IndexSetStore indexSetStore, IndexBlock[] blocks)
IIndex GetIndex(int offset)
Gets a mutable implementation of IIndex for the given index number in this set of indices...
An object that access to a set of indexes.
Definition: IIndexSet.cs:27
An interface for querying and accessing an index of primitive integers.
Definition: IIndex.cs:37
readonly IndexSetStore indexSetStore