couchbase-structures 0.0.4 → 0.0.9
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/.gitignore +1 -0
- data/lib/couchbase_structures/queue.rb +67 -8
- data/lib/couchbase_structures/stack.rb +47 -5
- data/lib/couchbase_structures/version.rb +1 -1
- metadata +3 -2
data/.gitignore
CHANGED
@@ -5,25 +5,44 @@ module CouchbaseStructures
|
|
5
5
|
include CouchbaseDocStore
|
6
6
|
|
7
7
|
def initialize(key)
|
8
|
-
@
|
9
|
-
@
|
10
|
-
@
|
8
|
+
@user_key = key
|
9
|
+
@key = "#{key}::queue"
|
10
|
+
@head_index_key = "#{@key}::head"
|
11
|
+
@tail_index_key = "#{@key}::tail"
|
12
|
+
initialize_document(@head_index_key, 0)
|
13
|
+
initialize_document(@tail_index_key, 0)
|
14
|
+
initialize_document(@key, { :type => "queue", :class => "CouchbaseStructures::Queue", :user_key => key } )
|
11
15
|
self
|
12
16
|
end
|
13
17
|
|
18
|
+
def inspect(html = false)
|
19
|
+
if html
|
20
|
+
"<strong>key</strong> = #{@key} <br /><strong>head_index</strong> = #{get_document(@head_index_key).to_s} <br /><strong>tail_index</strong> = #{get_document(@tail_index_key).to_s} <br /><strong>items</strong> = #{self.to_a(true)}"
|
21
|
+
else
|
22
|
+
"key = #{@key} head_index = #{get_document(@head_index_key).to_s} tail_index = #{get_document(@tail_index_key).to_s} items = #{self.to_a}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# peek at item at index (zero based)
|
27
|
+
def peek(queue_index)
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
# Add an item to the end of the queue (tail)
|
14
32
|
def enqueue(value)
|
15
33
|
new_tail_index = increase_atomic_count(@tail_index_key)
|
16
|
-
create_document("#{key}
|
34
|
+
create_document("#{@key}::#{new_tail_index}", value)
|
17
35
|
self
|
18
36
|
end
|
19
37
|
|
38
|
+
# Pop an item off the front of queue (head)
|
20
39
|
def pop()
|
21
40
|
head_index = get_document(@head_index_key)
|
22
41
|
tail_index = get_document(@tail_index_key)
|
23
|
-
if tail_index > head_index
|
24
|
-
increase_atomic_count(@head_index_key)
|
25
|
-
get_document("#{key}
|
26
|
-
else
|
42
|
+
if tail_index > head_index # if there is an item in the queue
|
43
|
+
increase_atomic_count(@head_index_key) #incremented new_head_index is ignored, but we are incrementing and popping value
|
44
|
+
return get_document("#{@key}::#{head_index + 1}")
|
45
|
+
else #
|
27
46
|
nil
|
28
47
|
end
|
29
48
|
end
|
@@ -31,5 +50,45 @@ module CouchbaseStructures
|
|
31
50
|
def size()
|
32
51
|
get_document(@tail_index_key) - get_document(@head_index_key)
|
33
52
|
end
|
53
|
+
|
54
|
+
def delete
|
55
|
+
#head_index = get_document(@head_index_key) # (not needed)
|
56
|
+
tail_index = get_document(@tail_index_key)
|
57
|
+
|
58
|
+
# delete all queued documents, including those that were before the current head (those aren't deleted as of now)
|
59
|
+
1.upto(tail_index) do |i|
|
60
|
+
delete_document("#{@key}::#{i}")
|
61
|
+
end
|
62
|
+
delete_document(@head_index_key)
|
63
|
+
delete_document(@tail_index_key)
|
64
|
+
delete_document(@key)
|
65
|
+
@user_key = nil
|
66
|
+
@key = nil
|
67
|
+
@head_index_key = nil
|
68
|
+
@tail_index_key = nil
|
69
|
+
nil
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_a(html=false)
|
73
|
+
a = []
|
74
|
+
head_index = get_document(@head_index_key) + 1
|
75
|
+
tail_index = get_document(@tail_index_key)
|
76
|
+
|
77
|
+
# delete all queued documents, including those that were before the current head (those aren't deleted as of now)
|
78
|
+
head_index.upto(tail_index) do |i|
|
79
|
+
a << get_document("#{@key}::#{i}")
|
80
|
+
end
|
81
|
+
|
82
|
+
if html
|
83
|
+
str = "["
|
84
|
+
a.each do |item|
|
85
|
+
str += "<br /> " + item.inspect
|
86
|
+
end
|
87
|
+
str += "<br />]"
|
88
|
+
return str
|
89
|
+
else
|
90
|
+
return a
|
91
|
+
end
|
92
|
+
end
|
34
93
|
end
|
35
94
|
end
|
@@ -5,29 +5,71 @@ module CouchbaseStructures
|
|
5
5
|
include CouchbaseDocStore
|
6
6
|
|
7
7
|
def initialize(key)
|
8
|
-
@
|
9
|
-
@
|
8
|
+
@user_key = key
|
9
|
+
@key = "#{@user_key}::stack"
|
10
|
+
@top_index_key = "#{@key}::top"
|
10
11
|
initialize_document(@top_index_key, 0)
|
12
|
+
initialize_document(@key, { :type => "stack", :class => "CouchbaseStructures::Stack", :user_key => @user_key } )
|
11
13
|
self
|
12
14
|
end
|
15
|
+
|
16
|
+
def inspect(html = false)
|
17
|
+
if html
|
18
|
+
"<strong>key</strong> = #{@key} <br /><strong>top_index</strong> = #{get_document(@top_index_key).to_s} <br /><strong>items</strong> = #{self.to_a(true)}"
|
19
|
+
else
|
20
|
+
"key = #{@key}\ntop_index = #{get_document(@top_index_key).to_s}\nitems = #{self.to_a}"
|
21
|
+
end
|
22
|
+
end
|
13
23
|
|
14
24
|
def push(value)
|
15
25
|
new_top_index = increase_atomic_count(@top_index_key)
|
16
|
-
create_document("#{key}
|
26
|
+
create_document("#{@key}::#{new_top_index}", value)
|
17
27
|
self
|
18
28
|
end
|
19
29
|
|
20
30
|
def pop()
|
21
31
|
old_top_index = get_document(@top_index_key)
|
32
|
+
return nil if @top_index_key == 0 # if the stack has returned to zero items
|
22
33
|
decrease_atomic_count(@top_index_key)
|
23
34
|
|
24
|
-
doc = get_document("#{key}
|
25
|
-
delete_document("#{key}
|
35
|
+
doc = get_document("#{@key}::#{old_top_index}")
|
36
|
+
delete_document("#{@key}::#{old_top_index}")
|
26
37
|
doc
|
27
38
|
end
|
28
39
|
|
29
40
|
def size
|
30
41
|
get_document(@top_index_key)
|
31
42
|
end
|
43
|
+
|
44
|
+
def delete
|
45
|
+
top_index = get_document(@top_index_key)
|
46
|
+
|
47
|
+
top_index.downto(1) do |i|
|
48
|
+
delete_document("#{@key}::#{i}")
|
49
|
+
end
|
50
|
+
delete_document(@top_index_key)
|
51
|
+
delete_document(@key)
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_a(html=false)
|
55
|
+
a = []
|
56
|
+
top_index = get_document(@top_index_key)
|
57
|
+
|
58
|
+
# delete all queued documents, including those that were before the current head (those aren't deleted as of now)
|
59
|
+
top_index.downto(1) do |i|
|
60
|
+
a << get_document("#{@key}::#{i}")
|
61
|
+
end
|
62
|
+
|
63
|
+
if html
|
64
|
+
str = "["
|
65
|
+
a.each do |item|
|
66
|
+
str += "<br /> " + item.inspect
|
67
|
+
end
|
68
|
+
str += "<br />]"
|
69
|
+
return str
|
70
|
+
else
|
71
|
+
return a
|
72
|
+
end
|
73
|
+
end
|
32
74
|
end
|
33
75
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: couchbase-structures
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
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-
|
12
|
+
date: 2012-11-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: couchbase-docstore
|
@@ -103,3 +103,4 @@ specification_version: 3
|
|
103
103
|
summary: A convenient implementation of stacks, queues and sorted lists using Couchbase
|
104
104
|
KV patterns.
|
105
105
|
test_files: []
|
106
|
+
has_rdoc:
|