Class: Bio::FlatFileIndex::DataBank
- Defined in:
- lib/bio/io/flatfile/index.rb
Overview
databank
Internal use only.
Instance Attribute Summary collapse
-
#always_check ⇒ Object
Returns the value of attribute always_check.
-
#dbname ⇒ Object
readonly
Returns the value of attribute dbname.
-
#index_type ⇒ Object
Returns the value of attribute index_type.
Class Method Summary collapse
Instance Method Summary collapse
- #bdb_open(*bdbarg) ⇒ Object
- #check_consistency ⇒ Object
- #close ⇒ Object
- #fileids ⇒ Object
- #format ⇒ Object
- #format=(str) ⇒ Object
- #get_flatfile_data(f, pos, length) ⇒ Object
- #init_fileids ⇒ Object
-
#initialize(name, idx_type = nil, hash = {}) ⇒ DataBank
constructor
A new instance of DataBank.
-
#primary ⇒ Object
parameters.
- #primary=(pri_name) ⇒ Object
- #search_all(key) ⇒ Object
- #search_all_get_unique_id(key) ⇒ Object
- #search_namespaces(key, *names) ⇒ Object
- #search_namespaces_get_unique_id(key, *names) ⇒ Object
- #search_primary(*arg) ⇒ Object
- #search_primary_get_unique_id(key) ⇒ Object
- #secondary ⇒ Object
- #secondary=(sec_names) ⇒ Object
- #to_s ⇒ Object
- #write(mode = 'wb', *bdbarg) ⇒ Object
Constructor Details
#initialize(name, idx_type = nil, hash = {}) ⇒ DataBank
Returns a new instance of DataBank.
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 |
# File 'lib/bio/io/flatfile/index.rb', line 1138 def initialize(name, idx_type = nil, hash = {}) @dbname = name.dup @dbname.freeze @bdb = nil @always_check = true self.index_type = (hash['index'] or idx_type) if @bdb then @config = BDBwrapper.new(@dbname, 'config') @bdb_fileids = BDBwrapper.new(@dbname, 'fileids') @nsclass_pri = BDB_1::PrimaryNameSpace @nsclass_sec = BDB_1::SecondaryNameSpace else @config = hash @nsclass_pri = Flat_1::PrimaryNameSpace @nsclass_sec = Flat_1::SecondaryNameSpace end true end |
Instance Attribute Details
#always_check ⇒ Object
Returns the value of attribute always_check.
1302 1303 1304 |
# File 'lib/bio/io/flatfile/index.rb', line 1302 def always_check @always_check end |
#dbname ⇒ Object (readonly)
Returns the value of attribute dbname.
1159 1160 1161 |
# File 'lib/bio/io/flatfile/index.rb', line 1159 def dbname @dbname end |
#index_type ⇒ Object
Returns the value of attribute index_type.
1159 1160 1161 |
# File 'lib/bio/io/flatfile/index.rb', line 1159 def index_type @index_type end |
Class Method Details
.filename(dbname) ⇒ Object
1121 1122 1123 |
# File 'lib/bio/io/flatfile/index.rb', line 1121 def self.filename(dbname) File.join(dbname, 'config.dat') end |
.open(*arg) ⇒ Object
1134 1135 1136 |
# File 'lib/bio/io/flatfile/index.rb', line 1134 def self.open(*arg) self.read(*arg) end |
.read(name, mode = 'rb', *bdbarg) ⇒ Object
1125 1126 1127 1128 1129 1130 1131 1132 |
# File 'lib/bio/io/flatfile/index.rb', line 1125 def self.read(name, mode = 'rb', *) f = File.open(filename(name), mode) hash = file2hash(f) f.close db = self.new(name, nil, hash) db.bdb_open(*) db end |
Instance Method Details
#bdb_open(*bdbarg) ⇒ Object
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 |
# File 'lib/bio/io/flatfile/index.rb', line 1194 def bdb_open(*) if @bdb then @config.close @config.open(*) @bdb_fileids.close @bdb_fileids.open(*) true else nil end end |
#check_consistency ⇒ Object
1364 1365 1366 |
# File 'lib/bio/io/flatfile/index.rb', line 1364 def check_consistency fileids.check_all end |
#close ⇒ Object
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 |
# File 'lib/bio/io/flatfile/index.rb', line 1224 def close DEBUG.print "DataBank: close #{@dbname}\n" primary.close secondary.close fileids.close if @bdb then @config.close @bdb_fileids.close end nil end |
#fileids ⇒ Object
1278 1279 1280 1281 1282 1283 |
# File 'lib/bio/io/flatfile/index.rb', line 1278 def fileids unless @fileids then init_fileids end @fileids end |
#format ⇒ Object
1271 1272 1273 1274 1275 1276 |
# File 'lib/bio/io/flatfile/index.rb', line 1271 def format unless @format then self.format = @config['format'] end @format end |
#format=(str) ⇒ Object
1267 1268 1269 |
# File 'lib/bio/io/flatfile/index.rb', line 1267 def format=(str) @format = str.to_s.dup end |
#get_flatfile_data(f, pos, length) ⇒ Object
1304 1305 1306 1307 1308 1309 1310 |
# File 'lib/bio/io/flatfile/index.rb', line 1304 def get_flatfile_data(f, pos, length) fi = fileids[f.to_i] if @always_check then raise "flatfile #{fi.filename.inspect} may be modified" unless fi.check end fi.get(pos.to_i, length.to_i) end |
#init_fileids ⇒ Object
1285 1286 1287 1288 1289 1290 1291 1292 |
# File 'lib/bio/io/flatfile/index.rb', line 1285 def init_fileids if @bdb then @fileids = FileIDs.new('', @bdb_fileids) else @fileids = FileIDs.new('fileid_', @config) end @fileids end |
#primary ⇒ Object
parameters
1237 1238 1239 1240 1241 1242 |
# File 'lib/bio/io/flatfile/index.rb', line 1237 def primary unless @primary then self.primary = @config['primary_namespace'] end @primary end |
#primary=(pri_name) ⇒ Object
1244 1245 1246 1247 1248 1249 1250 |
# File 'lib/bio/io/flatfile/index.rb', line 1244 def primary=(pri_name) if !pri_name or pri_name.empty? then pri_name = 'UNIQUE' end @primary = @nsclass_pri.new(@dbname, pri_name) @primary end |
#search_all(key) ⇒ Object
1331 1332 1333 1334 |
# File 'lib/bio/io/flatfile/index.rb', line 1331 def search_all(key) s = search_all_get_unique_id(key) search_primary(*s) end |
#search_all_get_unique_id(key) ⇒ Object
1312 1313 1314 1315 1316 1317 1318 1319 |
# File 'lib/bio/io/flatfile/index.rb', line 1312 def search_all_get_unique_id(key) s = secondary.search(key) p = primary.include?(key) s.push p if p s.sort! s.uniq! s end |
#search_namespaces(key, *names) ⇒ Object
1359 1360 1361 1362 |
# File 'lib/bio/io/flatfile/index.rb', line 1359 def search_namespaces(key, *names) s = search_namespaces_get_unique_id(key, *names) search_primary(*s) end |
#search_namespaces_get_unique_id(key, *names) ⇒ Object
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 |
# File 'lib/bio/io/flatfile/index.rb', line 1343 def search_namespaces_get_unique_id(key, *names) if names.include?(primary.name) then n2 = names.dup n2.delete(primary.name) p = primary.include?(key) else n2 = names p = nil end s = secondary.search_names(key, *n2) s.push p if p s.sort! s.uniq! s end |
#search_primary(*arg) ⇒ Object
1321 1322 1323 1324 1325 1326 1327 1328 1329 |
# File 'lib/bio/io/flatfile/index.rb', line 1321 def search_primary(*arg) r = Results.new arg.each do |x| a = primary.search(x) # a is empty or a.size==1 because primary key must be unique r.store(x, get_flatfile_data(*a[0])) unless a.empty? end r end |
#search_primary_get_unique_id(key) ⇒ Object
1336 1337 1338 1339 1340 1341 |
# File 'lib/bio/io/flatfile/index.rb', line 1336 def search_primary_get_unique_id(key) s = [] p = primary.include?(key) s.push p if p s end |
#secondary ⇒ Object
1252 1253 1254 1255 1256 1257 |
# File 'lib/bio/io/flatfile/index.rb', line 1252 def secondary unless @secondary then self.secondary = @config['secondary_namespaces'] end @secondary end |
#secondary=(sec_names) ⇒ Object
1259 1260 1261 1262 1263 1264 1265 |
# File 'lib/bio/io/flatfile/index.rb', line 1259 def secondary=(sec_names) if !sec_names then sec_names = [] end @secondary = NameSpaces.new(@dbname, @nsclass_sec, sec_names) @secondary end |
#to_s ⇒ Object
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 |
# File 'lib/bio/io/flatfile/index.rb', line 1177 def to_s a = "" a << "index\t#{@index_type}\n" unless @bdb then a << "format\t#{@format}\n" @fileids.each_with_index do |x, i| a << "#{x.to_s(i)}\n" end a << "primary_namespace\t#{@primary.name}\n" a << "secondary_namespaces\t" a << @secondary.names.join("\t") a << "\n" end a end |
#write(mode = 'wb', *bdbarg) ⇒ Object
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 |
# File 'lib/bio/io/flatfile/index.rb', line 1206 def write(mode = 'wb', *) unless FileTest.directory?(@dbname) then Dir.mkdir(@dbname) end f = File.open(self.class.filename(@dbname), mode) f.write self.to_s f.close if @bdb then bdb_open(*) @config['format'] = format @config['primary_namespace'] = @primary.name @config['secondary_namespaces'] = @secondary.names.join("\t") @bdb_fileids.writeback_array('', fileids, *) end true end |