Weitere ähnliche Inhalte
Ähnlich wie Agile Document Models & Data Structures
Ähnlich wie Agile Document Models & Data Structures (20)
Agile Document Models & Data Structures
- 6. ©2016 Couchbase Inc. ©2016 Couchbase Inc.
Simplified Data Structure Access
JSON
Doc CB Collec?ons
Framework
SDK
DS
6
App
“user1”: {“name”:... ,
“address”:.. ,
“favs”: [...]},
“user2”:{“name” ,
“address” ...,
”favs”: [...]},
for (String f : favs) {}
“user1”: {“name”:... ,
“address”:.. ,
“favs”: [...]},
“user2”:{“name” ,
“address” ...,
”favs”: [...]},
- 8. ©2016 Couchbase Inc. ©2016 Couchbase Inc.
The Four Data Structures…
Structure JSON Type JSON Example
Lists
- Append, prepend, insert
- Size/count
JSON Array: [… , ... ] [ 1, 2, “abc” ]
Maps
- Add/remove by key
- Size/count
JSON Object: { “key”:
“value”}
{ “name”: “value” }
Sets
- Specialized add/remove
- Unique values
- Size/count
JSON Array: [ … , ... ] [ 1, 3, 6, 8 ]
Queue
- First in – first out
- Pop – retrieve/remove
- Size/count
JSON Array: [… , ... ]
[ “task1”, “task2”, “task3” ]
remove 1...
[ “task2”, “task3”, “task4” ]
8
- 9. ©2016 Couchbase Inc. ©2016 Couchbase Inc.
Consistent Access Across Languages
Func:ons
Lists ListGet ListPush ListShift ListDelete ListSet ListSize
>namesList = bucket.ListGet(“key”)
>print namesList
[‘name1’,’name2’,’name3’]
Maps MapGet MapRemove MapSize MapSet
Sets SetAdd SetExists SetSize SetRemove
Queue QueuePush QueuePop QueueSize QueueRemove
9
• Idiomatic -vs- functional
• Java Collections Framework
• .NET System.Collections
• As well as functional approach
* Experimental features alert: may add/remove to this list – feedback welcome!
- 10. ©2016 Couchbase Inc. ©2016 Couchbase Inc.
Consistent Access Across Languages
10
Collec:ons Approach
Lists List<String> namesList = new CouchbaseArrayList<String>("key", bucket);
for (String name : namesList) { … }
Maps var namesDict = new CouchbaseDictionary<string, Poco>(_bucket, “key”);
namesDict.Add(“newkey1”, new Poco { Name = “poco1” });
Sets var namesSet = new CouchbaseSet<Poco>(_bucket, "pocos");
namesSet.Add(new Poco { Key = "poco1", Name = "Poco-pica" });
namesSet.Remove(new Poco {Key = "poco1", Name = "Poco-pica"});
foreach(var poco in namesSet){ … }
Queue var namesQueue = new CouchbaseQueue<Poco>(_bucket, key);
namesQueue.Enqueue(new Poco { Name = "pcoco1" });
var item = namesQueue.Peek();
• Support for advanced capabilities of collection frameworks
- 14. ©2016 Couchbase Inc. ©2016 Couchbase Inc.
Sub-Document API
14
Opera:ons
LookupIn LookupIn(key, operation(path))
Get Exists Execute
MutateIn MutateIn(key, operation(path, value))
Counter Insert Remove Replace Upsert Execute
arrayAddunique arrayAppend arrayInsert arrayPrepend
Chaining
Opera:ons
MutateIn(key, operation(path, value),
operation(path, value),
operation(path, value))
Returns SubdocResult<rc=0x0, key='map1', cas=0x14b6458980042,
specs=(Spec<GET, 'subkey1'>, Spec<EXISTS, 'subkey1'>),
results=[(0, u'subvalue1'), (0, None)]>
- 15. ©2016 Couchbase Inc. ©2016 Couchbase Inc.
Sample Sub-Document Lookup
15
LookupIn(key, operation(path))
LookupIn(‘copilotmark’)
.get(‘phones.number')
.execute();
LookupIn(‘copilotmark’)
.exists(‘phones’)
.get(‘phones.number')
.get(‘gender’)
.execute();
SubdocResult<rc=0x0, key=’copilotmark', cas=0x14b6458980042,
specs=(Spec<EXISTS, ‘phones’>, <GET, ’phones.number'>, <GET, ‘gender’),
results=[(0, None,), (0, ’212-771-1834’), (0, u’male')]>
- 35. ©2016 Couchbase Inc. ©2016 Couchbase Inc.
Example of View group_level = 1
Key Value
[2014] 36
[2015] 20
Execute Reduce
Key Value
[2014,11,29,18,49,36] 3
[2014,12,03,20,11,26] 5
[2014,12,03,23,37,21] 2
[2014,12,06,10,12,19] 8
[2014,12,09,05,01,26] 3
[2014,12,18,01,04,30] 11
[2014,12,26,18,34,44] 4
[2015,01,03,16,48,32] 7
[2015,01,03,20,20,06] 5
[2015,01,15,08,17,28] 8
Copyright © 2015 Couchbase, Inc. 35
• For the data below with Reduce function defined as _sum and group_level = 1
- 36. ©2016 Couchbase Inc. ©2016 Couchbase Inc.
Example of View group_level = 2
Key Value
[2014,11] 3
[2014,12] 33
[2015,01] 20
Key Value
[2014,11,29,18,49,36] 3
[2014,12,03,20,11,26] 5
[2014,12,03,23,37,21] 2
[2014,12,06,10,12,19] 8
[2014,12,09,05,01,26] 3
[2014,12,18,01,04,30] 11
[2014,12,26,18,34,44] 4
[2015,01,03,16,48,32] 7
[2015,01,03,20,20,06] 5
[2015,01,15,08,17,28] 8
Copyright © 2015 Couchbase, Inc. 36
• For the data below with Reduce function defined as _sum and group_level = 2
Execute Reduce
- 37. ©2016 Couchbase Inc. ©2016 Couchbase Inc.
Example of View group_level = 3
Key Value
[2014,11,29,18,49,36] 3
[2014,12,03,20,11,26] 5
[2014,12,03,23,37,21] 2
[2014,12,06,10,12,19] 8
[2014,12,09,05,01,26] 3
[2014,12,18,01,04,30] 11
[2014,12,26,18,34,44] 4
[2015,01,03,16,48,32] 7
[2015,01,03,20,20,06] 5
[2015,01,15,08,17,28] 8
Key Value
[2014,11,29] 3
[2014,12,03] 7
[2015,12,06] 8
[2015,12,09] 3
[2015,12,18] 11
[2015,12,26] 4
[2014,01,03] 12
[2014,01,15] 8
Copyright © 2015 Couchbase, Inc. 37
• For the data below with Reduce function defined as _sum and group_level = 3
Execute Reduce
- 39. ©2016 Couchbase Inc. ©2016 Couchbase Inc.
Empty, Null and Missing Property Values
39
{
countryCode: “UK”,
currencyCode: “GBP”,
region: “Europe”
}
{
countryCode: “UK”,
currencyCode: “GBP”,
region: “”
}
WHERE region
IS NOT MISSING, IS NOT NULL, IS VALUED
WHERE region
IS NOT MISSING, IS NOT NULL, IS NOT VALUED
{
countryCode: “UK”,
currencyCode: “GBP”
}
{
countryCode: “UK”,
currencyCode: “GBP”,
region: null
}
WHERE region IS MISSING WHERE region IS NULL