glyr 0.9.9.2 → 0.9.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/glyr.rb +11 -0
- data/lib/glyr/c/functions.rb +10 -0
- data/lib/glyr/c/types.rb +3 -1
- data/lib/glyr/database.rb +99 -0
- data/lib/glyr/query.rb +15 -1
- data/lib/glyr/results.rb +17 -1
- data/lib/glyr/version.rb +1 -1
- metadata +3 -2
data/lib/glyr.rb
CHANGED
@@ -12,6 +12,7 @@ require 'glyr/c'
|
|
12
12
|
|
13
13
|
require 'glyr/version'
|
14
14
|
require 'glyr/query'
|
15
|
+
require 'glyr/database'
|
15
16
|
require 'glyr/results'
|
16
17
|
require 'glyr/providers'
|
17
18
|
require 'glyr/sources'
|
@@ -25,4 +26,14 @@ module Glyr
|
|
25
26
|
def self.providers
|
26
27
|
@providers ||= Providers.create
|
27
28
|
end
|
29
|
+
|
30
|
+
def self.cache_at (path = nil)
|
31
|
+
if path
|
32
|
+
@database = Database.create(path).write!.read!
|
33
|
+
|
34
|
+
self
|
35
|
+
else
|
36
|
+
@database
|
37
|
+
end
|
38
|
+
end
|
28
39
|
end
|
data/lib/glyr/c/functions.rb
CHANGED
@@ -80,4 +80,14 @@ attach_function :glyr_get_requirements, [GetType], :int
|
|
80
80
|
|
81
81
|
attach_function :glyr_type_is_image, [GetType], :bool
|
82
82
|
|
83
|
+
attach_function :glyr_db_init, [:string], :pointer
|
84
|
+
attach_function :glyr_db_destroy, [:pointer], :void
|
85
|
+
attach_function :glyr_db_lookup, [:pointer, :pointer], :pointer
|
86
|
+
attach_function :glyr_db_insert, [:pointer, :pointer, :pointer], :void
|
87
|
+
attach_function :glyr_db_delete, [:pointer, :pointer], :int
|
88
|
+
attach_function :glyr_db_edit, [:pointer, :pointer, :pointer], :int
|
89
|
+
attach_function :glyr_db_replace, [:pointer, :pointer, :pointer, :pointer], :void
|
90
|
+
attach_function :glyr_db_foreach, [:pointer, :glyr_foreach_callback, :pointer], :void
|
91
|
+
attach_function :glyr_db_make_dummy, [], :pointer
|
92
|
+
|
83
93
|
end; end
|
data/lib/glyr/c/types.rb
CHANGED
@@ -98,9 +98,11 @@ class MemCache < FFI::Struct
|
|
98
98
|
:prev, :pointer
|
99
99
|
end
|
100
100
|
|
101
|
+
callback :glyr_foreach_callback, [:pointer, :pointer, :pointer], :int
|
102
|
+
|
101
103
|
class Database < FFI::Struct
|
102
104
|
layout \
|
103
|
-
:root_path, :
|
105
|
+
:root_path, :pointer,
|
104
106
|
:db_handle, :pointer
|
105
107
|
end
|
106
108
|
|
@@ -0,0 +1,99 @@
|
|
1
|
+
#--
|
2
|
+
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
3
|
+
# Version 2, December 2004
|
4
|
+
#
|
5
|
+
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
6
|
+
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
7
|
+
#
|
8
|
+
# 0. You just DO WHAT THE FUCK YOU WANT TO.
|
9
|
+
#++
|
10
|
+
|
11
|
+
module Glyr
|
12
|
+
|
13
|
+
class Database
|
14
|
+
# create a Query object with the passed options
|
15
|
+
def self.create (path)
|
16
|
+
pointer = C.glyr_db_init(path)
|
17
|
+
|
18
|
+
if pointer.null?
|
19
|
+
raise ArgumentError, "could not open/create database at #{path}"
|
20
|
+
end
|
21
|
+
|
22
|
+
wrap(pointer)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.wrap (pointer)
|
26
|
+
new(pointer).tap {|x|
|
27
|
+
ObjectSpace.define_finalizer x, finalizer(pointer)
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.finalizer (pointer) # :nodoc:
|
32
|
+
proc {
|
33
|
+
C.glyr_db_destroy(pointer)
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
include Enumerable
|
38
|
+
|
39
|
+
def initialize (pointer)
|
40
|
+
@internal = pointer.is_a?(FFI::Pointer) ? C::Database.new(pointer) : pointer
|
41
|
+
end
|
42
|
+
|
43
|
+
def path
|
44
|
+
to_native[:root_path]
|
45
|
+
end
|
46
|
+
|
47
|
+
def write?; !!@write; end
|
48
|
+
def read?; !!@read; end
|
49
|
+
|
50
|
+
def write!
|
51
|
+
@write = true
|
52
|
+
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def read!
|
57
|
+
@read = true
|
58
|
+
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
def lookup (query)
|
63
|
+
Results.wrap(C.glyr_db_lookup(to_native, query.to_native))
|
64
|
+
end
|
65
|
+
|
66
|
+
def insert (query, results)
|
67
|
+
C.glyr_db_insert(to_native, query.to_native, results.to_native)
|
68
|
+
|
69
|
+
self
|
70
|
+
end
|
71
|
+
|
72
|
+
def delete (query)
|
73
|
+
C.glyr_db_delete(to_native, query.to_native)
|
74
|
+
end
|
75
|
+
|
76
|
+
def replace (query, results)
|
77
|
+
C.glyr_db_edit(to_native, query.to_native, results.to_native)
|
78
|
+
end
|
79
|
+
|
80
|
+
def swap (query, a, b)
|
81
|
+
C.glyr_db_replace(to_native, a.respond_to?(:md5) ? a.md5 : a.to_s, query.to_native, b.to_native)
|
82
|
+
|
83
|
+
self
|
84
|
+
end
|
85
|
+
|
86
|
+
def each (&block)
|
87
|
+
C.glyr_db_foreach(to_native, proc {|query, item|
|
88
|
+
block.call [Query.new(query), Result.new(item)]
|
89
|
+
}, nil)
|
90
|
+
|
91
|
+
self
|
92
|
+
end
|
93
|
+
|
94
|
+
def to_native
|
95
|
+
@internal
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
data/lib/glyr/query.rb
CHANGED
@@ -197,6 +197,14 @@ class Query
|
|
197
197
|
end
|
198
198
|
end
|
199
199
|
|
200
|
+
def cache (value = nil)
|
201
|
+
if value
|
202
|
+
@cache = Database.create(value)
|
203
|
+
else
|
204
|
+
@cache
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
200
208
|
def download?
|
201
209
|
to_native[:download]
|
202
210
|
end
|
@@ -273,7 +281,13 @@ class Query
|
|
273
281
|
end
|
274
282
|
}, nil)
|
275
283
|
else
|
276
|
-
C.glyr_opt_dlcallback(to_native, nil, nil)
|
284
|
+
raise_if_error C.glyr_opt_dlcallback(to_native, nil, nil)
|
285
|
+
end
|
286
|
+
|
287
|
+
if db = cache || Glyr.cache_at
|
288
|
+
raise_if_error C.glyr_opt_lookup_db(to_native, db.to_native)
|
289
|
+
raise_if_error C.glyr_opt_db_autoread(to_native, true) if db.read?
|
290
|
+
raise_if_error C.glyr_opt_db_autowrite(to_native, true) if db.write?
|
277
291
|
end
|
278
292
|
|
279
293
|
result = C.glyr_get(to_native, error, length)
|
data/lib/glyr/results.rb
CHANGED
@@ -29,7 +29,19 @@ class Results
|
|
29
29
|
|
30
30
|
def initialize (pointer, length = nil)
|
31
31
|
@internal = pointer.is_a?(FFI::Pointer) ? C::MemCache.new(pointer) : pointer
|
32
|
-
@length = length
|
32
|
+
@length = if length
|
33
|
+
length
|
34
|
+
else
|
35
|
+
length = 0
|
36
|
+
current = @internal
|
37
|
+
|
38
|
+
until current.null?
|
39
|
+
length += 1
|
40
|
+
current = C::MemCache.new(current[:next])
|
41
|
+
end
|
42
|
+
|
43
|
+
length
|
44
|
+
end
|
33
45
|
end
|
34
46
|
|
35
47
|
def each (&block)
|
@@ -109,6 +121,10 @@ class Result
|
|
109
121
|
to_native[:type]
|
110
122
|
end
|
111
123
|
|
124
|
+
def md5
|
125
|
+
to_native[:md5sum]
|
126
|
+
end
|
127
|
+
|
112
128
|
def to_native
|
113
129
|
@internal
|
114
130
|
end
|
data/lib/glyr/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: glyr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.9.
|
4
|
+
version: 0.9.9.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ffi
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/glyr/c.rb
|
72
72
|
- lib/glyr/c/functions.rb
|
73
73
|
- lib/glyr/c/types.rb
|
74
|
+
- lib/glyr/database.rb
|
74
75
|
- lib/glyr/providers.rb
|
75
76
|
- lib/glyr/query.rb
|
76
77
|
- lib/glyr/results.rb
|