sldb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ require 'sldb'
2
+
3
+ #
4
+ # use the factory method to specify your database class
5
+ #
6
+ DB =
7
+ SLDB::class {
8
+ schema 'create table t ( a, b, c )'
9
+ path 'sldb'
10
+ }
11
+
12
+ #
13
+ # create and instance and use it - it will be created and initialized with the
14
+ # schema on the first use
15
+ #
16
+ db = DB::new
17
+
18
+ db.transaction do
19
+ db.execute 'insert into t values (0,1,2)'
20
+ db.execute('select * from t'){|tuple| p tuple}
21
+ end
22
+
@@ -0,0 +1,29 @@
1
+ require 'csv'
2
+ require 'sldb'
3
+
4
+ DB = SLDB::class :schema => 'create table t ( a, b, c )'
5
+ db = DB::new 'sldb'
6
+
7
+ #
8
+ # sldb uses arrayfields so many operations are natural since tuples are arrays,
9
+ # yet can be indexed by field
10
+ #
11
+ db.transaction do
12
+ db.execute "insert into t values ( 'A', 'B', 'C' )"
13
+ db.execute('select * from t') do |tuple|
14
+ puts "tuple => #{ tuple.inspect }"
15
+ tuple.fields.each{|f| puts " tuple[#{ f }] => #{ tuple[f] }"}
16
+ end
17
+ end
18
+
19
+ puts
20
+
21
+ #
22
+ # csv generation is an example of something which is much more natural with
23
+ # arrays
24
+ #
25
+ CSV::generate('csv') do |csv|
26
+ db.ro_transaction{db.execute('select * from t'){|t| csv << t}}
27
+ end
28
+ puts(IO::read('csv'))
29
+
@@ -0,0 +1,35 @@
1
+ require 'yaml'
2
+ require 'sldb'
3
+
4
+ DB = SLDB::new {
5
+ schema <<-sql
6
+ create table t0 ( a, b, c);
7
+ create table t1 ( x, y, z);
8
+ sql
9
+
10
+ path 'sldb'
11
+ }
12
+
13
+ db = DB::new
14
+
15
+ #
16
+ # many utility methods exist to make working with the databases easier
17
+ #
18
+ db.transaction do
19
+ db.tablenames.each do |tablename|
20
+ tuple = db.tuple_for tablename
21
+ tuple.fields.each{|f| tuple[f] = db.timestamp 'local' => true}
22
+ values = db.quote tuple
23
+ sql = "insert into #{ tablename } values (#{ values.join ',' })"
24
+ db.execute sql
25
+ end
26
+ end
27
+
28
+ db.read_only_transaction do
29
+ db.tablenames.each do |tablename|
30
+ db.execute("select * from #{ tablename }") do |t|
31
+ t.map!{|f| db.stamptime f, 'local' => true}
32
+ y t.to_hash
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ require 'yaml'
2
+ require 'sldb'
3
+
4
+ DB = SLDB::new { schema 'create table t ( tid, time )'; path 'sldb' }
5
+ db = DB::new
6
+
7
+ #
8
+ # multi-processed/multi-threaded applications may simoultaneously access the db
9
+ #
10
+
11
+ 4.times do
12
+ unless fork
13
+ pid = $$
14
+ threads = []
15
+ 2.times do |i|
16
+ threads <<
17
+ Thread::new(i, db) do |tid, db|
18
+ sleep rand
19
+ tuple = db.tuple_for 't'
20
+ tuple['tid'] = "#{ pid }:#{ tid }"
21
+ tuple['time'] = Time::now.to_f
22
+ values = db.quote tuple
23
+ db.transaction{db.execute "insert into t values(#{ values.join ',' })"}
24
+ end
25
+ end
26
+ threads.each{|t| t.join}
27
+ exit
28
+ end
29
+ end
30
+
31
+ 4.times{ Process::wait }
32
+
33
+ report = Hash::new{|h,k| h[k] = []}
34
+
35
+ db.transaction{db.execute("select * from t"){|t| report['t'] << t.to_hash}}
36
+
37
+ y report
@@ -0,0 +1,19 @@
1
+ require 'sldb'
2
+
3
+ #
4
+ # the SLDB factory method handles - as a special case - a pathname being passed
5
+ # in as meaning : create the class AND give me and instance of it which allows
6
+ # connecting to dbs even when the schema is unknown
7
+ #
8
+
9
+ dbklass = SLDB::new 'schema' => 'create table t ( answer )',
10
+ 'path' => 'sldb'
11
+ db = dbklass::new
12
+ db.transaction{ db.execute 'insert into t values ( 42 )' }
13
+
14
+ fork do
15
+ db = SLDB::new 'sldb' # here we don't know schema
16
+ db.transaction{ db.execute('select * from t'){|t| puts t['answer']}}
17
+ end
18
+
19
+ Process::wait
@@ -0,0 +1,49 @@
1
+ $VERBOSE=nil
2
+ $:.unshift 'lib'
3
+
4
+ require 'yaml'
5
+ require 'sldb'
6
+
7
+ DB = SLDB::new { schema 'create table t ( tid, time )'; path 'sldb' }
8
+ db = DB::new
9
+
10
+ loop do
11
+
12
+ #
13
+ # multi-processed/multi-threaded applications may simoultaneously access the db
14
+ #
15
+
16
+ 4.times do
17
+ unless fork
18
+ pid = $$
19
+ threads = []
20
+ 2.times do |i|
21
+ threads <<
22
+ Thread::new(i, db) do |tid, db|
23
+ sleep rand
24
+ tuple = db.tuple_for 't'
25
+ tuple['tid'] = "#{ pid }:#{ tid }"
26
+ tuple['time'] = Time::now.to_f
27
+ values = db.quote tuple
28
+ db.transaction{db.execute "insert into t values(#{ values.join ',' })"}
29
+ end
30
+ end
31
+ threads.each{|t| t.join}
32
+ exit
33
+ end
34
+ end
35
+
36
+ 4.times{ Process::wait }
37
+
38
+ tuples = nil
39
+
40
+ db.transaction{
41
+ tuples = db.execute("select * from t")
42
+ if tuples.size > 1024
43
+ db.execute("delete from t"){}
44
+ end
45
+ }
46
+
47
+ tuples.each_with_index{|tuple, idx| p [idx, tuple]}
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: sldb
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-10-23 00:00:00.000000 -06:00
8
+ summary: sldb
9
+ require_paths:
10
+ - lib
11
+ email: ara.t.howard@noaa.gov
12
+ homepage: http://codeforpeople.com/lib/ruby/sldb/
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: sldb
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ signing_key:
28
+ cert_chain:
29
+ authors:
30
+ - Ara T. Howard
31
+ files:
32
+ - lib
33
+ - install.rb
34
+ - README.tmpl
35
+ - white_box
36
+ - README
37
+ - gen_readme.rb
38
+ - VERSION
39
+ - gemspec.rb
40
+ - sample
41
+ - lib/sldb.rb
42
+ - lib/sldb-0.1.0.rb
43
+ - white_box/test.rb
44
+ - sample/a.rb
45
+ - sample/b.rb
46
+ - sample/c.rb
47
+ - sample/d.rb
48
+ - sample/e.rb
49
+ test_files: []
50
+ rdoc_options: []
51
+ extra_rdoc_files: []
52
+ executables: []
53
+ extensions: []
54
+ requirements: []
55
+ dependencies: []