<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dthomas</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dthomas"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Dthomas"/>
	<updated>2026-05-15T01:25:29Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74228</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74228"/>
		<updated>2013-03-21T02:25:14Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?&amp;lt;ref&amp;gt;http://www.eecg.toronto.edu/~moshovos/filter/doku.php?id=start#snoop_protocol_challenges&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;Experimental Analysis of Snoop Filters for MPSoC Embedded Systems By Aanjhan Ranganathan&amp;lt;/ref&amp;gt;A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
[[File:Sf.png]]&lt;br /&gt;
&lt;br /&gt;
Each bus segment in a processing unit can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
Another way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter&amp;lt;ref&amp;gt;E. Atoofian and A. Baniasadi, “Using Supplier Locality in Power-Aware Interconnects and Caches in Chip Multiprocessors”, Journal of Systems Architecture, vol. 54, no. 5, pp. 507-518, October 2007.&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters&amp;lt;ref&amp;gt;Strauss, K., Shen, X., and Torrellas, J. 2006. Flexible Snooping: Adaptive Forwarding and Filtering of Snoops in Embedded-Ring Multiprocessors. SIGARCH Comput. Archit. News 34, 2 (May. 2006), 327-338&amp;lt;/ref&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
[[File:SR-Filter.png]]&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters&amp;lt;ref&amp;gt;JETTY: Snoop Filtering for Reduced Energy Consumption in SMP Servers, Andreas Moshovos, Gokhan Memik, Babak Falsafi and Alok Choudhary, Proc. of HPCA-7 (Symposium on High Performance Computer Architecture), Jan. 2001.&amp;lt;/ref&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores&amp;lt;ref&amp;gt;Virtual Snooping: Filtering Snoops in Virtualized Multi-cores Daehoon Kim, Hwanju Kim, Jaehyuk Huh (KAIST), 43rd International Symposium on Microarchitecture, Dec. 2010&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example&amp;lt;ref&amp;gt;Valentina Salapura, Matthias A. Blumrich, Alan Gara: Design and implementation of the blue gene/P snoop filter. HPCA 2008&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP)&amp;lt;ref&amp;gt;Chinnakrishnan S. Ballapuram, Ahmad Sharif, and Hsien-Hsin S. Lee. “Exploiting Access Semantics and Program Behavior to Reduce Snoop Power in Chip Multiprocessors.” In Proceedings of the 13th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, pp.60-69, Seattle, WA, March, 2008.&amp;lt;/ref&amp;gt;. It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure 2 and figure 3, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
[[File:Blue gene-3.png]] &lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
[[File:Blue gene-4.png]]&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quiz==&lt;br /&gt;
&lt;br /&gt;
  1. Which of the following is a destination-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  2. Which one of the following is a source-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  3. Which one of the following protocols does not belong to either Destination-based or Source-based protocols?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  4. Which one of the following snoop filtering protocols reduces the number of snoop broadcasts?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  5. Choose one of the following which fall in the category of exclusive snoop filters?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. Blue Gene/P Range Filter&lt;br /&gt;
&lt;br /&gt;
  6. Which of the following is/are challenges faced by snoop-based protocosl?&lt;br /&gt;
    a. Request ordering&lt;br /&gt;
    b. High network/bus bandwidth requirement.&lt;br /&gt;
    c. High tag look-up bandwidth requirements&lt;br /&gt;
    d. All of the above&lt;br /&gt;
&lt;br /&gt;
  7. What is snooping?&lt;br /&gt;
    a. The process where the individual caches monitor address lines for accesses to memory locations that they have cached.&lt;br /&gt;
    b. The process of monitoring all bus transactions.&lt;br /&gt;
    c. The process of monitoring memory locations to see if write happens to these memory locations.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  8. Which one of the following is/are use(s) of snoop filters?&lt;br /&gt;
    a. To reduce bus bandwidth requirements.&lt;br /&gt;
    b. To reduce power consumption.&lt;br /&gt;
    c. To reduce contention between processor and snooper to access cache tag.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  9. What kind of cache is used in the snoop filter implemented in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based &lt;br /&gt;
    b. Counting Stream Register based&lt;br /&gt;
    c. Stream Register based&lt;br /&gt;
    d. Vector-Exclusive-JETTY based&lt;br /&gt;
&lt;br /&gt;
  10. What keeps track of what is there in the cache in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based snoop filter&lt;br /&gt;
    b. Counting Stream Register snoop filter&lt;br /&gt;
    c. Stream Register Snoop filter&lt;br /&gt;
    d. Vector-Exclusive-JETTY snoop filter&lt;br /&gt;
&lt;br /&gt;
Answers: 1-a, 2-b , 3-d , 4-b , 5-d, 6-d, 7-a, 8-d, 9-d, 10-c&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:SR-Filter.png&amp;diff=74227</id>
		<title>File:SR-Filter.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:SR-Filter.png&amp;diff=74227"/>
		<updated>2013-03-21T02:24:30Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: Stream register snoop filter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Stream register snoop filter&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74224</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74224"/>
		<updated>2013-03-21T02:21:17Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?&amp;lt;ref&amp;gt;http://www.eecg.toronto.edu/~moshovos/filter/doku.php?id=start#snoop_protocol_challenges&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;Experimental Analysis of Snoop Filters for MPSoC Embedded Systems By Aanjhan Ranganathan&amp;lt;/ref&amp;gt;A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
[[File:Sf.png]]&lt;br /&gt;
&lt;br /&gt;
Each bus segment in a processing unit can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
Another way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter&amp;lt;ref&amp;gt;E. Atoofian and A. Baniasadi, “Using Supplier Locality in Power-Aware Interconnects and Caches in Chip Multiprocessors”, Journal of Systems Architecture, vol. 54, no. 5, pp. 507-518, October 2007.&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters&amp;lt;ref&amp;gt;Strauss, K., Shen, X., and Torrellas, J. 2006. Flexible Snooping: Adaptive Forwarding and Filtering of Snoops in Embedded-Ring Multiprocessors. SIGARCH Comput. Archit. News 34, 2 (May. 2006), 327-338&amp;lt;/ref&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters&amp;lt;ref&amp;gt;JETTY: Snoop Filtering for Reduced Energy Consumption in SMP Servers, Andreas Moshovos, Gokhan Memik, Babak Falsafi and Alok Choudhary, Proc. of HPCA-7 (Symposium on High Performance Computer Architecture), Jan. 2001.&amp;lt;/ref&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores&amp;lt;ref&amp;gt;Virtual Snooping: Filtering Snoops in Virtualized Multi-cores Daehoon Kim, Hwanju Kim, Jaehyuk Huh (KAIST), 43rd International Symposium on Microarchitecture, Dec. 2010&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example&amp;lt;ref&amp;gt;Valentina Salapura, Matthias A. Blumrich, Alan Gara: Design and implementation of the blue gene/P snoop filter. HPCA 2008&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP)&amp;lt;ref&amp;gt;Chinnakrishnan S. Ballapuram, Ahmad Sharif, and Hsien-Hsin S. Lee. “Exploiting Access Semantics and Program Behavior to Reduce Snoop Power in Chip Multiprocessors.” In Proceedings of the 13th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, pp.60-69, Seattle, WA, March, 2008.&amp;lt;/ref&amp;gt;. It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure 2 and figure 3, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
[[File:Blue gene-3.png]] &lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
[[File:Blue gene-4.png]]&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quiz==&lt;br /&gt;
&lt;br /&gt;
  1. Which of the following is a destination-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  2. Which one of the following is a source-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  3. Which one of the following protocols does not belong to either Destination-based or Source-based protocols?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  4. Which one of the following snoop filtering protocols reduces the number of snoop broadcasts?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  5. Choose one of the following which fall in the category of exclusive snoop filters?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. Blue Gene/P Range Filter&lt;br /&gt;
&lt;br /&gt;
  6. Which of the following is/are challenges faced by snoop-based protocosl?&lt;br /&gt;
    a. Request ordering&lt;br /&gt;
    b. High network/bus bandwidth requirement.&lt;br /&gt;
    c. High tag look-up bandwidth requirements&lt;br /&gt;
    d. All of the above&lt;br /&gt;
&lt;br /&gt;
  7. What is snooping?&lt;br /&gt;
    a. The process where the individual caches monitor address lines for accesses to memory locations that they have cached.&lt;br /&gt;
    b. The process of monitoring all bus transactions.&lt;br /&gt;
    c. The process of monitoring memory locations to see if write happens to these memory locations.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  8. Which one of the following is/are use(s) of snoop filters?&lt;br /&gt;
    a. To reduce bus bandwidth requirements.&lt;br /&gt;
    b. To reduce power consumption.&lt;br /&gt;
    c. To reduce contention between processor and snooper to access cache tag.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  9. What kind of cache is used in the snoop filter implemented in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based &lt;br /&gt;
    b. Counting Stream Register based&lt;br /&gt;
    c. Stream Register based&lt;br /&gt;
    d. Vector-Exclusive-JETTY based&lt;br /&gt;
&lt;br /&gt;
  10. What keeps track of what is there in the cache in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based snoop filter&lt;br /&gt;
    b. Counting Stream Register snoop filter&lt;br /&gt;
    c. Stream Register Snoop filter&lt;br /&gt;
    d. Vector-Exclusive-JETTY snoop filter&lt;br /&gt;
&lt;br /&gt;
Answers: 1-a, 2-b , 3-d , 4-b , 5-d, 6-d, 7-a, 8-d, 9-d, 10-c&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Blue_gene-4.png&amp;diff=74223</id>
		<title>File:Blue gene-4.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Blue_gene-4.png&amp;diff=74223"/>
		<updated>2013-03-21T02:20:58Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: BlueGene snoop filter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;BlueGene snoop filter&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Blue_gene-3.png&amp;diff=74221</id>
		<title>File:Blue gene-3.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Blue_gene-3.png&amp;diff=74221"/>
		<updated>2013-03-21T02:20:01Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: BlueGene snoop filter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;BlueGene snoop filter&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Blue_gene-2.png&amp;diff=74219</id>
		<title>File:Blue gene-2.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Blue_gene-2.png&amp;diff=74219"/>
		<updated>2013-03-21T02:14:44Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: BlueGene snoop filter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;BlueGene snoop filter&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74217</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74217"/>
		<updated>2013-03-21T02:12:45Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?&amp;lt;ref&amp;gt;http://www.eecg.toronto.edu/~moshovos/filter/doku.php?id=start#snoop_protocol_challenges&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;Experimental Analysis of Snoop Filters for MPSoC Embedded Systems By Aanjhan Ranganathan&amp;lt;/ref&amp;gt;A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.[[File:Sf.png]]&lt;br /&gt;
&lt;br /&gt;
Each bus segment in a processing unit can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
Another way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter&amp;lt;ref&amp;gt;E. Atoofian and A. Baniasadi, “Using Supplier Locality in Power-Aware Interconnects and Caches in Chip Multiprocessors”, Journal of Systems Architecture, vol. 54, no. 5, pp. 507-518, October 2007.&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters&amp;lt;ref&amp;gt;Strauss, K., Shen, X., and Torrellas, J. 2006. Flexible Snooping: Adaptive Forwarding and Filtering of Snoops in Embedded-Ring Multiprocessors. SIGARCH Comput. Archit. News 34, 2 (May. 2006), 327-338&amp;lt;/ref&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters&amp;lt;ref&amp;gt;JETTY: Snoop Filtering for Reduced Energy Consumption in SMP Servers, Andreas Moshovos, Gokhan Memik, Babak Falsafi and Alok Choudhary, Proc. of HPCA-7 (Symposium on High Performance Computer Architecture), Jan. 2001.&amp;lt;/ref&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores&amp;lt;ref&amp;gt;Virtual Snooping: Filtering Snoops in Virtualized Multi-cores Daehoon Kim, Hwanju Kim, Jaehyuk Huh (KAIST), 43rd International Symposium on Microarchitecture, Dec. 2010&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example&amp;lt;ref&amp;gt;Valentina Salapura, Matthias A. Blumrich, Alan Gara: Design and implementation of the blue gene/P snoop filter. HPCA 2008&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP)&amp;lt;ref&amp;gt;Chinnakrishnan S. Ballapuram, Ahmad Sharif, and Hsien-Hsin S. Lee. “Exploiting Access Semantics and Program Behavior to Reduce Snoop Power in Chip Multiprocessors.” In Proceedings of the 13th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, pp.60-69, Seattle, WA, March, 2008.&amp;lt;/ref&amp;gt;. It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quiz==&lt;br /&gt;
&lt;br /&gt;
  1. Which of the following is a destination-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  2. Which one of the following is a source-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  3. Which one of the following protocols does not belong to either Destination-based or Source-based protocols?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  4. Which one of the following snoop filtering protocols reduces the number of snoop broadcasts?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  5. Choose one of the following which fall in the category of exclusive snoop filters?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. Blue Gene/P Range Filter&lt;br /&gt;
&lt;br /&gt;
  6. Which of the following is/are challenges faced by snoop-based protocosl?&lt;br /&gt;
    a. Request ordering&lt;br /&gt;
    b. High network/bus bandwidth requirement.&lt;br /&gt;
    c. High tag look-up bandwidth requirements&lt;br /&gt;
    d. All of the above&lt;br /&gt;
&lt;br /&gt;
  7. What is snooping?&lt;br /&gt;
    a. The process where the individual caches monitor address lines for accesses to memory locations that they have cached.&lt;br /&gt;
    b. The process of monitoring all bus transactions.&lt;br /&gt;
    c. The process of monitoring memory locations to see if write happens to these memory locations.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  8. Which one of the following is/are use(s) of snoop filters?&lt;br /&gt;
    a. To reduce bus bandwidth requirements.&lt;br /&gt;
    b. To reduce power consumption.&lt;br /&gt;
    c. To reduce contention between processor and snooper to access cache tag.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  9. What kind of cache is used in the snoop filter implemented in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based &lt;br /&gt;
    b. Counting Stream Register based&lt;br /&gt;
    c. Stream Register based&lt;br /&gt;
    d. Vector-Exclusive-JETTY based&lt;br /&gt;
&lt;br /&gt;
  10. What keeps track of what is there in the cache in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based snoop filter&lt;br /&gt;
    b. Counting Stream Register snoop filter&lt;br /&gt;
    c. Stream Register Snoop filter&lt;br /&gt;
    d. Vector-Exclusive-JETTY snoop filter&lt;br /&gt;
&lt;br /&gt;
Answers: 1-a, 2-b , 3-d , 4-b , 5-d, 6-d, 7-a, 8-d, 9-d, 10-c&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Blue_gene-1.png&amp;diff=74215</id>
		<title>File:Blue gene-1.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Blue_gene-1.png&amp;diff=74215"/>
		<updated>2013-03-21T02:11:14Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: BlueGene Snoop Filter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;BlueGene Snoop Filter&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74212</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74212"/>
		<updated>2013-03-21T02:07:59Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?&amp;lt;ref&amp;gt;http://www.eecg.toronto.edu/~moshovos/filter/doku.php?id=start#snoop_protocol_challenges&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;Experimental Analysis of Snoop Filters for MPSoC Embedded Systems By Aanjhan Ranganathan&amp;lt;/ref&amp;gt;A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.[[File:Sf.png|200px|thumb|right|alt Snoop Filter]]&lt;br /&gt;
&lt;br /&gt;
Each bus segment in a processing unit can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
Another way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter&amp;lt;ref&amp;gt;E. Atoofian and A. Baniasadi, “Using Supplier Locality in Power-Aware Interconnects and Caches in Chip Multiprocessors”, Journal of Systems Architecture, vol. 54, no. 5, pp. 507-518, October 2007.&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters&amp;lt;ref&amp;gt;Strauss, K., Shen, X., and Torrellas, J. 2006. Flexible Snooping: Adaptive Forwarding and Filtering of Snoops in Embedded-Ring Multiprocessors. SIGARCH Comput. Archit. News 34, 2 (May. 2006), 327-338&amp;lt;/ref&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters&amp;lt;ref&amp;gt;JETTY: Snoop Filtering for Reduced Energy Consumption in SMP Servers, Andreas Moshovos, Gokhan Memik, Babak Falsafi and Alok Choudhary, Proc. of HPCA-7 (Symposium on High Performance Computer Architecture), Jan. 2001.&amp;lt;/ref&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores&amp;lt;ref&amp;gt;Virtual Snooping: Filtering Snoops in Virtualized Multi-cores Daehoon Kim, Hwanju Kim, Jaehyuk Huh (KAIST), 43rd International Symposium on Microarchitecture, Dec. 2010&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example&amp;lt;ref&amp;gt;Valentina Salapura, Matthias A. Blumrich, Alan Gara: Design and implementation of the blue gene/P snoop filter. HPCA 2008&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP)&amp;lt;ref&amp;gt;Chinnakrishnan S. Ballapuram, Ahmad Sharif, and Hsien-Hsin S. Lee. “Exploiting Access Semantics and Program Behavior to Reduce Snoop Power in Chip Multiprocessors.” In Proceedings of the 13th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, pp.60-69, Seattle, WA, March, 2008.&amp;lt;/ref&amp;gt;. It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quiz==&lt;br /&gt;
&lt;br /&gt;
  1. Which of the following is a destination-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  2. Which one of the following is a source-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  3. Which one of the following protocols does not belong to either Destination-based or Source-based protocols?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  4. Which one of the following snoop filtering protocols reduces the number of snoop broadcasts?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  5. Choose one of the following which fall in the category of exclusive snoop filters?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. Blue Gene/P Range Filter&lt;br /&gt;
&lt;br /&gt;
  6. Which of the following is/are challenges faced by snoop-based protocosl?&lt;br /&gt;
    a. Request ordering&lt;br /&gt;
    b. High network/bus bandwidth requirement.&lt;br /&gt;
    c. High tag look-up bandwidth requirements&lt;br /&gt;
    d. All of the above&lt;br /&gt;
&lt;br /&gt;
  7. What is snooping?&lt;br /&gt;
    a. The process where the individual caches monitor address lines for accesses to memory locations that they have cached.&lt;br /&gt;
    b. The process of monitoring all bus transactions.&lt;br /&gt;
    c. The process of monitoring memory locations to see if write happens to these memory locations.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  8. Which one of the following is/are use(s) of snoop filters?&lt;br /&gt;
    a. To reduce bus bandwidth requirements.&lt;br /&gt;
    b. To reduce power consumption.&lt;br /&gt;
    c. To reduce contention between processor and snooper to access cache tag.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  9. What kind of cache is used in the snoop filter implemented in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based &lt;br /&gt;
    b. Counting Stream Register based&lt;br /&gt;
    c. Stream Register based&lt;br /&gt;
    d. Vector-Exclusive-JETTY based&lt;br /&gt;
&lt;br /&gt;
  10. What keeps track of what is there in the cache in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based snoop filter&lt;br /&gt;
    b. Counting Stream Register snoop filter&lt;br /&gt;
    c. Stream Register Snoop filter&lt;br /&gt;
    d. Vector-Exclusive-JETTY snoop filter&lt;br /&gt;
&lt;br /&gt;
Answers: 1-a, 2-b , 3-d , 4-b , 5-d, 6-d, 7-a, 8-d, 9-d, 10-c&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Sf.png&amp;diff=74211</id>
		<title>File:Sf.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Sf.png&amp;diff=74211"/>
		<updated>2013-03-21T02:06:29Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: Snoop Filter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Snoop Filter&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Snoop_filter.png&amp;diff=74205</id>
		<title>File:Snoop filter.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Snoop_filter.png&amp;diff=74205"/>
		<updated>2013-03-21T02:03:19Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: Snoop filter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Snoop filter&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74188</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74188"/>
		<updated>2013-03-21T01:49:23Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?&amp;lt;ref&amp;gt;http://www.eecg.toronto.edu/~moshovos/filter/doku.php?id=start#snoop_protocol_challenges&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;Experimental Analysis of Snoop Filters for MPSoC Embedded Systems By Aanjhan Ranganathan&amp;lt;/ref&amp;gt;A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
Each bus segment in a processing unit can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
Another way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter&amp;lt;ref&amp;gt;E. Atoofian and A. Baniasadi, “Using Supplier Locality in Power-Aware Interconnects and Caches in Chip Multiprocessors”, Journal of Systems Architecture, vol. 54, no. 5, pp. 507-518, October 2007.&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters&amp;lt;ref&amp;gt;Strauss, K., Shen, X., and Torrellas, J. 2006. Flexible Snooping: Adaptive Forwarding and Filtering of Snoops in Embedded-Ring Multiprocessors. SIGARCH Comput. Archit. News 34, 2 (May. 2006), 327-338&amp;lt;/ref&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters&amp;lt;ref&amp;gt;JETTY: Snoop Filtering for Reduced Energy Consumption in SMP Servers, Andreas Moshovos, Gokhan Memik, Babak Falsafi and Alok Choudhary, Proc. of HPCA-7 (Symposium on High Performance Computer Architecture), Jan. 2001.&amp;lt;/ref&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores&amp;lt;ref&amp;gt;Virtual Snooping: Filtering Snoops in Virtualized Multi-cores Daehoon Kim, Hwanju Kim, Jaehyuk Huh (KAIST), 43rd International Symposium on Microarchitecture, Dec. 2010&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example&amp;lt;ref&amp;gt;Valentina Salapura, Matthias A. Blumrich, Alan Gara: Design and implementation of the blue gene/P snoop filter. HPCA 2008&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP)&amp;lt;ref&amp;gt;Chinnakrishnan S. Ballapuram, Ahmad Sharif, and Hsien-Hsin S. Lee. “Exploiting Access Semantics and Program Behavior to Reduce Snoop Power in Chip Multiprocessors.” In Proceedings of the 13th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, pp.60-69, Seattle, WA, March, 2008.&amp;lt;/ref&amp;gt;. It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quiz==&lt;br /&gt;
&lt;br /&gt;
  1. Which of the following is a destination-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  2. Which one of the following is a source-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  3. Which one of the following protocols does not belong to either Destination-based or Source-based protocols?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  4. Which one of the following snoop filtering protocols reduces the number of snoop broadcasts?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  5. Choose one of the following which fall in the category of exclusive snoop filters?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. Blue Gene/P Range Filter&lt;br /&gt;
&lt;br /&gt;
  6. Which of the following is/are challenges faced by snoop-based protocosl?&lt;br /&gt;
    a. Request ordering&lt;br /&gt;
    b. High network/bus bandwidth requirement.&lt;br /&gt;
    c. High tag look-up bandwidth requirements&lt;br /&gt;
    d. All of the above&lt;br /&gt;
&lt;br /&gt;
  7. What is snooping?&lt;br /&gt;
    a. The process where the individual caches monitor address lines for accesses to memory locations that they have cached.&lt;br /&gt;
    b. The process of monitoring all bus transactions.&lt;br /&gt;
    c. The process of monitoring memory locations to see if write happens to these memory locations.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  8. Which one of the following is/are use(s) of snoop filters?&lt;br /&gt;
    a. To reduce bus bandwidth requirements.&lt;br /&gt;
    b. To reduce power consumption.&lt;br /&gt;
    c. To reduce contention between processor and snooper to access cache tag.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  9. What kind of cache is used in the snoop filter implemented in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based &lt;br /&gt;
    b. Counting Stream Register based&lt;br /&gt;
    c. Stream Register based&lt;br /&gt;
    d. Vector-Exclusive-JETTY based&lt;br /&gt;
&lt;br /&gt;
  10. What keeps track of what is there in the cache in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based snoop filter&lt;br /&gt;
    b. Counting Stream Register snoop filter&lt;br /&gt;
    c. Stream Register Snoop filter&lt;br /&gt;
    d. Vector-Exclusive-JETTY snoop filter&lt;br /&gt;
&lt;br /&gt;
Answers: 1-a, 2-b , 3-d , 4-b , 5-d, 6-d, 7-a, 8-d, 9-d, 10-c&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74187</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74187"/>
		<updated>2013-03-21T01:48:59Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?&amp;lt;ref&amp;gt;http://www.eecg.toronto.edu/~moshovos/filter/doku.php?id=start#snoop_protocol_challenges&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;Experimental Analysis of Snoop Filters for MPSoC Embedded Systems By Aanjhan Ranganathan&amp;lt;/ref&amp;gt;A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
Each bus segment in a processing unit can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
Another way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter&amp;lt;ref&amp;gt;E. Atoofian and A. Baniasadi, “Using Supplier Locality in Power-Aware Interconnects and Caches in Chip Multiprocessors”, Journal of Systems Architecture, vol. 54, no. 5, pp. 507-518, October 2007.&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters&amp;lt;ref&amp;gt;Strauss, K., Shen, X., and Torrellas, J. 2006. Flexible Snooping: Adaptive Forwarding and Filtering of Snoops in Embedded-Ring Multiprocessors. SIGARCH Comput. Archit. News 34, 2 (May. 2006), 327-338&amp;lt;/ref&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters&amp;lt;ref&amp;gt;JETTY: Snoop Filtering for Reduced Energy Consumption in SMP Servers, Andreas Moshovos, Gokhan Memik, Babak Falsafi and Alok Choudhary, Proc. of HPCA-7 (Symposium on High Performance Computer Architecture), Jan. 2001.&amp;lt;/ref&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores&amp;lt;ref&amp;gt;Virtual Snooping: Filtering Snoops in Virtualized Multi-cores Daehoon Kim, Hwanju Kim, Jaehyuk Huh (KAIST), 43rd International Symposium on Microarchitecture, Dec. 2010&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example&amp;lt;ref&amp;gt;Valentina Salapura, Matthias A. Blumrich, Alan Gara: Design and implementation of the blue gene/P snoop filter. HPCA 2008&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP)&amp;lt;ref&amp;gt;Chinnakrishnan S. Ballapuram, Ahmad Sharif, and Hsien-Hsin S. Lee. “Exploiting Access Semantics and Program Behavior to Reduce Snoop Power in Chip Multiprocessors.” In Proceedings of the 13th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, pp.60-69, Seattle, WA, March, 2008.&amp;lt;/ref&amp;gt;. It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quiz==&lt;br /&gt;
&lt;br /&gt;
  1. Which of the following is a destination-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  2. Which one of the following is a source-based snoop filtering protocol?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  3. Which one of the following protocols does not belong to either Destination-based or Source-based protocols?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Software-Assisted Filters&lt;br /&gt;
    d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
  4. Which one of the following snoop filtering protocols reduces the number of snoop broadcasts?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Speculative Selective Requests&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
  5. Choose one of the following which fall in the category of exclusive snoop filters?&lt;br /&gt;
    a. Stream Register based Snoop Protocol&lt;br /&gt;
    b. Software-Assisted Filters&lt;br /&gt;
    c. Serial Snooping Protocol&lt;br /&gt;
    d. Blue Gene/P Range Filter&lt;br /&gt;
&lt;br /&gt;
  6. Which of the following is/are challenges faced by snoop-based protocosl?&lt;br /&gt;
    a. Request ordering&lt;br /&gt;
    b. High network/bus bandwidth requirement.&lt;br /&gt;
    c. High tag look-up bandwidth requirements&lt;br /&gt;
    d. All of the above&lt;br /&gt;
&lt;br /&gt;
  7. What is snooping?&lt;br /&gt;
    a. The process where the individual caches monitor address lines for accesses to memory locations that they have cached.&lt;br /&gt;
    b. The process of monitoring all bus transactions.&lt;br /&gt;
    c. The process of monitoring memory locations to see if write happens to these memory locations.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  8. Which one of the following is/are use(s) of snoop filters?&lt;br /&gt;
    a. To reduce bus bandwidth requirements.&lt;br /&gt;
    b. To reduce power consumption.&lt;br /&gt;
    c. To reduce contention between processor and snooper to access cache tag.&lt;br /&gt;
    d. All of the above.&lt;br /&gt;
&lt;br /&gt;
  9. What kind of cache is used in the snoop filter implemented in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based &lt;br /&gt;
    b. Counting Stream Register based&lt;br /&gt;
    c. Stream Register based&lt;br /&gt;
    d. Vector-Exclusive-JETTY based&lt;br /&gt;
&lt;br /&gt;
  10. What keeps track of what is there in the cache in BlueGene supercomputer?&lt;br /&gt;
    a. Speculative Selective Requests-based snoop filter&lt;br /&gt;
    b. Counting Stream Register snoop filter&lt;br /&gt;
    c. Stream Register Snoop filter&lt;br /&gt;
    d. Vector-Exclusive-JETTY snoop filter&lt;br /&gt;
&lt;br /&gt;
Answers 1-a, 2-b , 3-d , 4-b , 5-d, 6-d, 7-a, 8-d, 9-d, 10-c&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74180</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74180"/>
		<updated>2013-03-21T01:40:56Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?&amp;lt;ref&amp;gt;http://www.eecg.toronto.edu/~moshovos/filter/doku.php?id=start#snoop_protocol_challenges&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;Experimental Analysis of Snoop Filters for MPSoC Embedded Systems By Aanjhan Ranganathan&amp;lt;/ref&amp;gt;A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
Each bus segment in a processing unit can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
Another way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter&amp;lt;ref&amp;gt;E. Atoofian and A. Baniasadi, “Using Supplier Locality in Power-Aware Interconnects and Caches in Chip Multiprocessors”, Journal of Systems Architecture, vol. 54, no. 5, pp. 507-518, October 2007.&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters&amp;lt;ref&amp;gt;Strauss, K., Shen, X., and Torrellas, J. 2006. Flexible Snooping: Adaptive Forwarding and Filtering of Snoops in Embedded-Ring Multiprocessors. SIGARCH Comput. Archit. News 34, 2 (May. 2006), 327-338&amp;lt;/ref&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters&amp;lt;ref&amp;gt;JETTY: Snoop Filtering for Reduced Energy Consumption in SMP Servers, Andreas Moshovos, Gokhan Memik, Babak Falsafi and Alok Choudhary, Proc. of HPCA-7 (Symposium on High Performance Computer Architecture), Jan. 2001.&amp;lt;/ref&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores&amp;lt;ref&amp;gt;Virtual Snooping: Filtering Snoops in Virtualized Multi-cores Daehoon Kim, Hwanju Kim, Jaehyuk Huh (KAIST), 43rd International Symposium on Microarchitecture, Dec. 2010&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example&amp;lt;ref&amp;gt;Valentina Salapura, Matthias A. Blumrich, Alan Gara: Design and implementation of the blue gene/P snoop filter. HPCA 2008&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP)&amp;lt;ref&amp;gt;Chinnakrishnan S. Ballapuram, Ahmad Sharif, and Hsien-Hsin S. Lee. “Exploiting Access Semantics and Program Behavior to Reduce Snoop Power in Chip Multiprocessors.” In Proceedings of the 13th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, pp.60-69, Seattle, WA, March, 2008.&amp;lt;/ref&amp;gt;. It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Quiz==&lt;br /&gt;
&lt;br /&gt;
1. Which of the following is a destination-based snoop filtering protocol?&lt;br /&gt;
&lt;br /&gt;
a. Stream Register based Snoop Protocol&lt;br /&gt;
b. Speculative Selective Requests&lt;br /&gt;
c. Software-Assisted Filters&lt;br /&gt;
d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
2. Which one of the following is a source-based snoop filtering protocol?&lt;br /&gt;
&lt;br /&gt;
a. Stream Register based Snoop Protocol&lt;br /&gt;
b. Software-Assisted Filters&lt;br /&gt;
c. Serial Snooping Protocol&lt;br /&gt;
d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
3. Which one of the following protocols does not belong to either Destination-based or Source-based protocols?&lt;br /&gt;
&lt;br /&gt;
a. Stream Register based Snoop Protocol&lt;br /&gt;
b. Speculative Selective Requests&lt;br /&gt;
c. Software-Assisted Filters&lt;br /&gt;
d. Serial Snooping Protocol&lt;br /&gt;
&lt;br /&gt;
4. Which one of the following snoop filtering protocols reduces the number of snoop broadcasts?&lt;br /&gt;
&lt;br /&gt;
a. Stream Register based Snoop Protocol&lt;br /&gt;
b. Speculative Selective Requests&lt;br /&gt;
c. Serial Snooping Protocol&lt;br /&gt;
d. In-Network Snoop Filtering&lt;br /&gt;
&lt;br /&gt;
5. Choose one of the following which fall in the category of exclusive snoop filters?&lt;br /&gt;
&lt;br /&gt;
a. Stream Register based Snoop Protocol&lt;br /&gt;
b. Software-Assisted Filters&lt;br /&gt;
c. Serial Snooping Protocol&lt;br /&gt;
d. Blue Gene/P Range Filter&lt;br /&gt;
&lt;br /&gt;
6. Which of the following is/are challenges faced by snoop-based protocosl?&lt;br /&gt;
&lt;br /&gt;
a. Request ordering&lt;br /&gt;
b. High network/bus bandwidth requirement.&lt;br /&gt;
c. High tag look-up bandwidth requirements&lt;br /&gt;
d. All of the above&lt;br /&gt;
&lt;br /&gt;
7. What is snooping?&lt;br /&gt;
&lt;br /&gt;
a. The process where the individual caches monitor address lines for accesses to memory locations that they have cached.&lt;br /&gt;
b. The process of monitoring all bus transactions.&lt;br /&gt;
c. The process of monitoring memory locations to see if write happens to these memory locations.&lt;br /&gt;
d. All of the above.&lt;br /&gt;
&lt;br /&gt;
8. Which one of the following is/are use(s) of snoop filters?&lt;br /&gt;
&lt;br /&gt;
a. To reduce bus bandwidth requirements.&lt;br /&gt;
b. To reduce power consumption.&lt;br /&gt;
c. To reduce contention between processor and snooper to access cache tag.&lt;br /&gt;
d. All of the above.&lt;br /&gt;
&lt;br /&gt;
9. What kind of snoop cache is used in the snoop filter implemented in BlueGene supercomputer?&lt;br /&gt;
&lt;br /&gt;
a. Speculative Selective Requests-based &lt;br /&gt;
b. Counting Stream Register based&lt;br /&gt;
c. Stream Register based&lt;br /&gt;
d. Vector-Exclusive-JETTY based&lt;br /&gt;
&lt;br /&gt;
10. What keeps track of what is there in the cache in BlueGene supercomputer?&lt;br /&gt;
&lt;br /&gt;
a. Speculative Selective Requests-based snoop filter&lt;br /&gt;
b. Counting Stream Register snoop filter&lt;br /&gt;
c. Stream Register Snoop filter&lt;br /&gt;
d. Vector-Exclusive-JETTY snoop filter&lt;br /&gt;
&lt;br /&gt;
Answers 1-a, 2-b , 3-d , 4-b , 5-d, 6-d, 7-a, 8-d, 9-d, 10-c&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74148</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74148"/>
		<updated>2013-03-21T01:20:13Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: /* Snoop Filter as a solution */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?&amp;lt;ref&amp;gt;http://www.eecg.toronto.edu/~moshovos/filter/doku.php?id=start#snoop_protocol_challenges&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;Experimental Analysis of Snoop Filters for MPSoC Embedded Systems By Aanjhan Ranganathan&amp;lt;/ref&amp;gt;A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
Each bus segment in a processing unit can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
Another way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter&amp;lt;ref&amp;gt;E. Atoofian and A. Baniasadi, “Using Supplier Locality in Power-Aware Interconnects and Caches in Chip Multiprocessors”, Journal of Systems Architecture, vol. 54, no. 5, pp. 507-518, October 2007.&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters&amp;lt;ref&amp;gt;Strauss, K., Shen, X., and Torrellas, J. 2006. Flexible Snooping: Adaptive Forwarding and Filtering of Snoops in Embedded-Ring Multiprocessors. SIGARCH Comput. Archit. News 34, 2 (May. 2006), 327-338&amp;lt;/ref&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters&amp;lt;ref&amp;gt;JETTY: Snoop Filtering for Reduced Energy Consumption in SMP Servers, Andreas Moshovos, Gokhan Memik, Babak Falsafi and Alok Choudhary, Proc. of HPCA-7 (Symposium on High Performance Computer Architecture), Jan. 2001.&amp;lt;/ref&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores&amp;lt;ref&amp;gt;Virtual Snooping: Filtering Snoops in Virtualized Multi-cores Daehoon Kim, Hwanju Kim, Jaehyuk Huh (KAIST), 43rd International Symposium on Microarchitecture, Dec. 2010&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example&amp;lt;ref&amp;gt;Valentina Salapura, Matthias A. Blumrich, Alan Gara: Design and implementation of the blue gene/P snoop filter. HPCA 2008&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP)&amp;lt;ref&amp;gt;Chinnakrishnan S. Ballapuram, Ahmad Sharif, and Hsien-Hsin S. Lee. “Exploiting Access Semantics and Program Behavior to Reduce Snoop Power in Chip Multiprocessors.” In Proceedings of the 13th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, pp.60-69, Seattle, WA, March, 2008.&amp;lt;/ref&amp;gt;. It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74143</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74143"/>
		<updated>2013-03-21T01:19:14Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?&amp;lt;ref&amp;gt;http://www.eecg.toronto.edu/~moshovos/filter/doku.php?id=start#snoop_protocol_challenges&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;Experimental Analysis of Snoop Filters for MPSoC Embedded Systems By Aanjhan Ranganathan&amp;lt;/ref&amp;gt;A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
Each bus segment can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
Another way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter&amp;lt;ref&amp;gt;E. Atoofian and A. Baniasadi, “Using Supplier Locality in Power-Aware Interconnects and Caches in Chip Multiprocessors”, Journal of Systems Architecture, vol. 54, no. 5, pp. 507-518, October 2007.&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters&amp;lt;ref&amp;gt;Strauss, K., Shen, X., and Torrellas, J. 2006. Flexible Snooping: Adaptive Forwarding and Filtering of Snoops in Embedded-Ring Multiprocessors. SIGARCH Comput. Archit. News 34, 2 (May. 2006), 327-338&amp;lt;/ref&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters&amp;lt;ref&amp;gt;JETTY: Snoop Filtering for Reduced Energy Consumption in SMP Servers, Andreas Moshovos, Gokhan Memik, Babak Falsafi and Alok Choudhary, Proc. of HPCA-7 (Symposium on High Performance Computer Architecture), Jan. 2001.&amp;lt;/ref&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores&amp;lt;ref&amp;gt;Virtual Snooping: Filtering Snoops in Virtualized Multi-cores Daehoon Kim, Hwanju Kim, Jaehyuk Huh (KAIST), 43rd International Symposium on Microarchitecture, Dec. 2010&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example&amp;lt;ref&amp;gt;Valentina Salapura, Matthias A. Blumrich, Alan Gara: Design and implementation of the blue gene/P snoop filter. HPCA 2008&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP)&amp;lt;ref&amp;gt;Chinnakrishnan S. Ballapuram, Ahmad Sharif, and Hsien-Hsin S. Lee. “Exploiting Access Semantics and Program Behavior to Reduce Snoop Power in Chip Multiprocessors.” In Proceedings of the 13th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, pp.60-69, Seattle, WA, March, 2008.&amp;lt;/ref&amp;gt;. It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74135</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74135"/>
		<updated>2013-03-21T01:10:44Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?&amp;lt;ref&amp;gt;http://www.eecg.toronto.edu/~moshovos/filter/doku.php?id=start#snoop_protocol_challenges&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
Each bus segment can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;Experimental Analysis of Snoop Filters for MPSoC Embedded Systems By Aanjhan Ranganathan&amp;lt;/ref&amp;gt;A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
One way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter&amp;lt;ref&amp;gt;E. Atoofian and A. Baniasadi, “Using Supplier Locality in Power-Aware Interconnects and Caches in Chip Multiprocessors”, Journal of Systems Architecture, vol. 54, no. 5, pp. 507-518, October 2007.&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters&amp;lt;ref&amp;gt;Strauss, K., Shen, X., and Torrellas, J. 2006. Flexible Snooping: Adaptive Forwarding and Filtering of Snoops in Embedded-Ring Multiprocessors. SIGARCH Comput. Archit. News 34, 2 (May. 2006), 327-338&amp;lt;/ref&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters&amp;lt;ref&amp;gt;JETTY: Snoop Filtering for Reduced Energy Consumption in SMP Servers, Andreas Moshovos, Gokhan Memik, Babak Falsafi and Alok Choudhary, Proc. of HPCA-7 (Symposium on High Performance Computer Architecture), Jan. 2001.&amp;lt;/ref&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores&amp;lt;ref&amp;gt;Virtual Snooping: Filtering Snoops in Virtualized Multi-cores Daehoon Kim, Hwanju Kim, Jaehyuk Huh (KAIST), 43rd International Symposium on Microarchitecture, Dec. 2010&amp;lt;/ref&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example&amp;lt;ref&amp;gt;Valentina Salapura, Matthias A. Blumrich, Alan Gara: Design and implementation of the blue gene/P snoop filter. HPCA 2008&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP)&amp;lt;ref&amp;gt;Chinnakrishnan S. Ballapuram, Ahmad Sharif, and Hsien-Hsin S. Lee. “Exploiting Access Semantics and Program Behavior to Reduce Snoop Power in Chip Multiprocessors.” In Proceedings of the 13th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, pp.60-69, Seattle, WA, March, 2008.&amp;lt;/ref&amp;gt;. It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74126</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74126"/>
		<updated>2013-03-21T00:40:31Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having [http://en.wikipedia.org/wiki/Shared_memory shared memory] and each processor having its own private [http://en.wikipedia.org/wiki/Cache cache] is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bus_sniffing Snooping] is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper has snooped the bus transaction and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many [http://en.wikipedia.org/wiki/Cache_coherence coherence protocols] are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses. The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance. In addition, the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
Each bus segment can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state [http://en.wikipedia.org/wiki/MESI_protocol (MESI)] of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache. The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
One way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large [http://en.wikipedia.org/wiki/Multiprocessing multiprocessors], these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a look-up if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A [http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/set.html set-associative] array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the [http://en.wikipedia.org/wiki/Blue_Gene IBM BlueGene] supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM. The compute [http://en.wikipedia.org/wiki/Application-specific_integrated_circuit ASIC] is a highly integrated [http://en.wikipedia.org/wiki/System_on_a_chip System-on-a-Chip] (SoC) [http://en.wikipedia.org/wiki/Multi-core_processor chip multiprocessor] (CMP). It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline [http://en.wikipedia.org/wiki/SIMD SIMD] [http://en.wikipedia.org/wiki/Floating-point_unit floating-point unit] and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the BlueGene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache of the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network [http://en.wikipedia.org/wiki/Direct_memory_access DMA] by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY&amp;lt;ref&amp;gt;http://cucis.eecs.northwestern.edu/publications/pdf/MosMem01A.pdf&amp;lt;/ref&amp;gt; records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's [http://en.wikipedia.org/wiki/CPU_cache L1 cache], the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the [http://en.wikipedia.org/wiki/Symmetric_multiprocessing SMP-on-a-chip]. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74123</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74123"/>
		<updated>2013-03-20T23:59:31Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having shared memory and each processor having its own private cache is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
Snooping is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper snooped the bus transaction and and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many coherence protocols are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses.The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance.In addition the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
Each bus segment can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state (MESI) of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache.&lt;br /&gt;
The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
One way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large multiprocessors, these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a lookup if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A set-associative array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
An example of snoop filter implementations can be found in the IBM BlueGene supercomputer. The Blue Gene/P supercomputer is a scalable distributed-memory system consisting of up to 262,144 nodes. Each node is built around a single compute ASIC with 2GB or 4 GB of external DDR2 DRAM.The compute ASIC is a highly integrated System-on-a-Chip (SoC) chip multiprocessor (CMP). It contains four PowerPC 450 embedded processor cores, each with private, highly-associative, 32 KB rst-level instruction and data caches. Each core is coupled to a dual-pipeline SIMD floating-point unit and to a small, private, second-level cache whose principal responsibility is to prefetch streams of data. In addition, the chip integrates an 8 MB, shared third-level cache, two memory controllers, five network controllers, and a performance monitor. &lt;br /&gt;
&lt;br /&gt;
Data integrity between the Blue Gene/P cores is maintained with a cache coherence protocol based on write-invalidates, with all L1-caches operating in write-through mode. Every store not only updates the L1-cache ofSnoop Filter as a solution&lt;br /&gt;
&lt;br /&gt;
Each bus segment can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state (MESI) of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a superset of blocks currently present in the cache or those that are not present in the cache.&lt;br /&gt;
The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
One way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
 the issuing core, but also sends the write data via the L2 write buffer to the shared L3 cache. The L2s broadcast an invalidate request for the write address to ensure that no stale copy of the same datum will remain in the other L1s and L2s. A snoop filter is introduced at each of the four processor located outside the L1 caches. &lt;br /&gt;
&lt;br /&gt;
Each snoop filter receives invalidation requests from three remote cores and the network DMA by way of a point-to-point interconnect, so it must process requests from four memory writers concurrently. To handle these simultaneous requests, we implement a separate snoop filter block, or port filter, for each interconnect port. Thus, coherency requests on all ports are processed concurrently, and a small fraction of all requests are forwarded to the processor. As shown in figure, there are four separate port filters, each of which handles requests from one remoter processor.&lt;br /&gt;
&lt;br /&gt;
The snoop cache is essentially a Vector-Exclusive-JETTY records blocks that have been snooped recently (thus invalidated in the cache). It consists of a small, direct-mapped array, where an entry is created for each snoop request. A subsequent snoop request for the same block will match in the snoop cache and be filtered. If the block is loaded in the processor's L1 cache, the corresponding entry is removed from the snoop cache, and any new snoop request to the same block will miss in the snoop cache and be forwarded to the L1 cache.&lt;br /&gt;
&lt;br /&gt;
Unlike the snoop cache that keeps track of what is not in the cache, the stream register filter keeps track of what is in the cache. More precisely, the stream registers keep track of the lines that are in the cache, but may assume that some lines are cached which are not actually there. The heart of the filter is the stream registers themselves, of which there are a small number. One of these registers is updated with the line address every time the cache loads a new line. A particular register is chosen for update based upon the current stream register state and the address of the new line being loaded into the cache.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
With the advent of commodity multi-core processors, we have entered the era of the SMP-on-a-chip. These high-performance systems will generate an enormous amount of shared memory traffic, so it will be important to eliminate as much of the useless inter-processor snooping as possible. In addition, power dissipation has become a major factor with increased chip density, so mechanisms to eliminate useless coherence actions will be important.&lt;br /&gt;
&lt;br /&gt;
In this wiki page we have discussed how snoop filters solve the above mentioned issues. We have also discussed the architectures of different types of snoop filters and how snoop filters are implemented in complex multi-core processors such as the IBM BlueGene supercomputer. In conclusion snoop filter can be used as an effective solution to some of the challenges such as large bandwidth requirements and request ordering faced by snoop-based protocols .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74122</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74122"/>
		<updated>2013-03-20T23:57:29Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having shared memory and each processor having its own private cache is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
Snooping is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper snooped the bus transaction and and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many coherence protocols are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses.The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance.In addition the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
Each bus segment can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state (MESI) of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache.&lt;br /&gt;
The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
One way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large multiprocessors, these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a lookup if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al. Filters'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A set-associative array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74121</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74121"/>
		<updated>2013-03-20T23:56:15Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having shared memory and each processor having its own private cache is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
Snooping is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols at each node in a bus-based multiprocessor system can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block.&amp;lt;ref&amp;gt;Fundamentals of Prallel Computer Architecture by Yan Solihin&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper snooped the bus transaction and and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many coherence protocols are snoop based.&lt;br /&gt;
&lt;br /&gt;
===Challenges faced by Snoop-based protocols===&lt;br /&gt;
&lt;br /&gt;
Snoop protocols face three main challenges:&lt;br /&gt;
&lt;br /&gt;
'''1. Request ordering:''' &lt;br /&gt;
&lt;br /&gt;
It is necessary to ensure the relative order of snoop broadcasts. This can be achieved in small multiprocessors by relying on network ordering properties. But larger multiprocessors with arbitrary network topologies cannot rely on the network to order requests.&lt;br /&gt;
&lt;br /&gt;
'''2. Network bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
Snoop-based protocols require to do snoop broadcasts during all cache misses.The snoops consume large amounts of network bandwidth and this is the main factor that limits the scalability of snoop coherence protocols. &lt;br /&gt;
&lt;br /&gt;
'''3. Tag look-up bandwidth requirements:'''&lt;br /&gt;
&lt;br /&gt;
In addition to network bandwidth, each broadcast consumes significant tag look-up bandwidth as each cache checks to see if it has a copy of the requested block. As the number of cores in the system grows, the rate of snoop-induced tag look-ups can cause contention that delays demand look-ups from the local core and hurts performance.In addition the challenge of simply providing sufficient network and tag look-up bandwidth, it is important to consider the energy consumed in the network and in the tag arrays as a result of snoop broadcasts.&lt;br /&gt;
&lt;br /&gt;
===Snoop Filter as a solution===&lt;br /&gt;
&lt;br /&gt;
Each bus segment can be separated into different cache coherency domains using a snoop filter, with very little traffic occurring between the two. The snoop filter is most likely implemented as a large table that stores recent cache line requests, the state (MESI) of each cache line, and bits to indicate which segment the cache line is in (or both). When a cache miss occurs, the originating CPU broadcasts a snoop request on its bus. Both the snoop filter and the other CPU in the package will receive the request and take action appropriately. If the read request hits in the snoop filter, then it will check where the requested cache line is located. If the requested cache line is only available on the other bus segment, then the snoop request will be sent to the other segment. If the requested cache line is available on both buses or only on the originating CPU’s bus or only in main memory, then the snoop filter does not pass along the request, thereby saving front side bus bandwidth. &lt;br /&gt;
&lt;br /&gt;
A snoop filter is a small cache-like structure present between the data cache and the bus as shown in figure, which filters probable data cache misses and forwards those snoop induced look-ups that might result in a hit. The snoop filters achieve filtering by either keeping track of a super-set of blocks currently present in the cache or those that are not present in the cache.&lt;br /&gt;
The most important factor to be considered while designing these filters is that a look-up in any of these filters must consume less energy than a normal cache look-up. Several snoop filter architectures have been proposed in previous literature depending on the filtering methodology.&lt;br /&gt;
&lt;br /&gt;
One way to implement the snoop filter is to duplicate the cache tag array and filter out all snoop requests that miss. But this is not practical and can result in serious performance bottlenecks. But, it has been shown that very accurate filtering can be achieved with small designs that conservatively approximate the cache contents and operate at a reasonable frequency.&lt;br /&gt;
&lt;br /&gt;
==Types of Snoop Filters==&lt;br /&gt;
&lt;br /&gt;
There are many different attributes that can be used to categorize these filters. We chose to use the point of origin as the first order attribute for classification. These filters can be grouped into three broad classes:&lt;br /&gt;
&lt;br /&gt;
===Destination Based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
Destination-based snoop filters are the ones those are inclined to reduce the number of tag look-ups as a result of various snoop broadcasts. These in effect don’t reduce the number of snoop broadcasts; however they try to optimize the actions taken in response to it. They achieve this by filtering the snoop requests and hence avoid local tag look-ups. This optimization technique tries to reduce energy and bandwidth utilization.&lt;br /&gt;
&lt;br /&gt;
On receiving a snoop request, a these category of snoop filters might give off immediate response without requiring a tag array look-up. This goes in to reduce the energy consumption and also the contention for the tag array. For general purpose large multiprocessors, these tag look-ups can easily out-number demand look-ups from the local processor, giving way to high contention and a loss of performance so it is important to try to reduce that contention.&lt;br /&gt;
&lt;br /&gt;
====Atoofian and Baniasadi Filter====&lt;br /&gt;
&lt;br /&gt;
This is a kind of filter needing very little area and energy overhead. It maintains a table consisting of saturating counters at each cache with one counter for each processor core in the system. When a snoop request is received for reading, it only performs a lookup if the counter corresponding to the requesting core is “saturated”. If not so, it replays with an negative acknowledgement. There might be another core with a positive reply or if the requester may get all negative acknowledgements. In that case, it tries again and this all of them are force to do look-ups. A “saturating” counter is always incremented when a cache supplies data in response to a snoop request from the corresponding core, and it is reset to zero when a snoop request from the corresponding core fails to find the data in the cache. This protocol serves good in case of workloads that exhibit supplier locality. They reduce energy and contention traffic. However if the workload does not exhibit supplier locality, this may have significant performance losses. An example of such filter is SPLAS-2.&lt;br /&gt;
&lt;br /&gt;
====Inclusive Filters====&lt;br /&gt;
&lt;br /&gt;
Inclusive filters keep a track of all lines that result in positive responses to snoop requests. In other words, an inclusive snoop filter keeps track of a super-set of the blocks that are cached. This ensures a miss in an inclusive filter is guaranteed to miss in the cache, so there is no need to forward the request. Similarly, a hit in an inclusive snoop filter may or may not hit in the cache, so the request must be forwarded. The pivotal challenge here is to provide an area and energy-efficient structure that can represent this set of blocks.&lt;br /&gt;
&lt;br /&gt;
=====Subset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Strauss et al.'''&lt;br /&gt;
&lt;br /&gt;
Strauss et al. filters focus on filtering snoops for read requests and track those blocks that are in a “supplier” state and hence could give a positive response to a snoop request. A filter by the name of “subset” filter keeps track of the subset of blocks in the cache that are in one of the “supplier” states. As a result of which lines in shared state will return a negative reply to a read snoop. A set-associative array contains the tags of all supplier blocks in the cache. This at times might result in increased bandwidth utilization and also the subset filter only keeps track of lines in order to do efficient tag-look up for read snoop requests only and it does filter the invalidate messages.&lt;br /&gt;
&lt;br /&gt;
=====Superset Filters=====&lt;br /&gt;
&lt;br /&gt;
'''Stream Register Snoop Filters'''&lt;br /&gt;
This was introduced by IBM Researchers which went on to be used the Blue Gene/P supercomputer. This uses stream registers to encode cache lines stored in the cache in particular way as mentioned in the following text. Each of these stream register (SR) is consists of the following; a base register, a mask register, and a valid bit. The base register here keeps the starting point of a line to be reached out, while the mask register encodes the entries of the block that have been accessed as offsets of the base. Due to space constraints, the offsets are not represented explicitly. Rather, the mask represents a super-set of the offsets that have been accessed.&lt;br /&gt;
&lt;br /&gt;
'''Counting Stream Register Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
The disadvantage with the above filter is over time, as more and more unique addresses are accessed by the filter, more and more of the bits in the mask will be set to 0. Hence, all possible addresses that the stream register can filter will decrease over time. Gradually, all of the mask bits become zero, and the SR filters no further addresses. At this point in time, even if the address that were accessed long back are being evicted out or invalidated then also they will not be filtered buy the snoop filter. This happens until a cache wrap occurs and all of them are flushed out to start afresh. The important point is this whole flushing part results in significant amount of overheads.&lt;br /&gt;
&lt;br /&gt;
Counting Stream Register Snoop Filters overcome this by keeping a counter value instead of the valid bit used in the original Stream Register based Snoop Filters. Each time a particular block offset address in accessed, in addition to making the corresponding super-set of bits to 0, it also increments the counter value by 1. And every time a cache invalidation or eviction occurs, it decrements the counter value by 1. So by using this logic, the overhead of flushing out the register in case of cache wrap is not required. It simply checks the counter value and filters it if the value is 0.  &lt;br /&gt;
&lt;br /&gt;
====Exclusive Filters====&lt;br /&gt;
&lt;br /&gt;
In contrast to the inclusive filters mentioned above, an exclusive snoop filter maintains information about blocks that are not being cached. A hit here ensures that the cache does not contain the block, so there is no need to forward the request. However a miss must be forwarded to the cache for processing.&lt;br /&gt;
&lt;br /&gt;
=====Exclude-Jetty Filters=====&lt;br /&gt;
&lt;br /&gt;
This is an exclusive filter where a set-associative table containing the most recently snooped addresses that returned negative responses is maintained. This technique benefits due to the principle of temporal locality in snoop addresses, filtering out most unnecessary snoops for highly contended blocks. The Blue Gene/P system incorporates a variation of this in the name of “vector exclude Jetty” as part of its snoop filter.&lt;br /&gt;
&lt;br /&gt;
=====Blue Gene/P Range Filter=====&lt;br /&gt;
&lt;br /&gt;
The Blue Gene/P has a different kind of exclusive snoop filter namely “range filter”. This filter keeps track of a range of addresses that are either outside the range of the pertinent cache or they are not cached. Hence the snoop requests for this range of addresses are ignored. The range filter is software-controlled and performs well when multiple processors are known to be using completely distinct and continuous portions of physical memory.&lt;br /&gt;
&lt;br /&gt;
===Source-based Snoop Filters===&lt;br /&gt;
&lt;br /&gt;
The destination-based snoop filters filter out unnecessary tag lookups but these requests and their corresponding responses still require bandwidth. Hence if the requesters can somehow able to predict in advance either that no other caches will have copies of the requested block, or that only certain cache might have copies, then it can avoid sending a snoop broadcast all together, or potentially send a multicast instead of a full broadcast. This is the technique used by so called source-based snoop filters. This also results in decreased bandwidth utilization and better scaling snoop protocol to many cores.&lt;br /&gt;
&lt;br /&gt;
====Speculative Selective Requests-based Snoop Filters====&lt;br /&gt;
&lt;br /&gt;
Speculative selective requests (SSR) uses the idea of keeping a “saturating counter” and a supplier ID information at each core to predict the supplier cache from where to ask required block when needed. It starts incrementing the counter corresponding to a particular core every time a block is provided by the same core. When this counter reaches a saturating value, the cache controller further approaches this pertinent supplier directly for the block when needed. Until then it uses the broadcast mechanism. For workloads that exhibit a large degree of supplier locality, such as SPLASH-2, data is often streamed from one cache to another, making SSR very effective. However, other workloads with less predictable behavior see little benefit from SSR as the counter's seldom saturate, or potentially suffer performance losses if frequent miss predictions occur.&lt;br /&gt;
&lt;br /&gt;
====Software-Assisted Filters====&lt;br /&gt;
&lt;br /&gt;
Various software based source-based snoop filters that avoided broadcasting snoops for certain accesses where those snoops were known to fail were also proposed. The first filter is based on the fact that data in the stack of each thread are private and are not shared hence snoop request for these need not be sent. Another proposed filter mechanism uses a “Snoop-Me-Not” bit to all instructions that access memory. Programmers, compilers and operating systems aid in setting the bits appropriately depending on the type of application. This technique requires minimal architectural support, but needs the programmer or compiler to understand the memory layout of the program, and possibly deal with issues.&lt;br /&gt;
&lt;br /&gt;
===Other Categories filters===&lt;br /&gt;
&lt;br /&gt;
====Serial Snooping====&lt;br /&gt;
Serial-snooping is a form of protocol-based filtering where snoop messages are exchanged between involved processors until a valid copy of the data is found. In this way, it reduces the number of messages exchanged. However this may not be the case always. The benefits of serial snooping also largely depend on the network topology in which it is being used. In some cases latency may be reduced while in other cases this may lead to unnecessary overheads.&lt;br /&gt;
&lt;br /&gt;
====In-Network Snoop Filtering====&lt;br /&gt;
&lt;br /&gt;
Consider small networks with each network comprising of a set of caches. The networks here are interconnected by routers. The idea used in In-Network Snoop Filtering is to modify the outgoing snoop broadcasts from a network depending on some predefined criteria and the information regarding the networks where to send the request are stored in a table at each outgoing router of a network. A RegionTracker cache is used for the same which tracks which regions are present in each cache. In other words, each router uses a small table to track the sharing information for the most recently accessed regions. Hence a snoop broadcast is only sent to those regions who actually share the relevant block. The major setback in this approach is that in-network filtering apparently is not compatible with non-adaptive routing protocols.&lt;br /&gt;
&lt;br /&gt;
====Snoop Filtering in Virtualized Multi-Cores====&lt;br /&gt;
&lt;br /&gt;
In virtual environments, usually the sharing tends to occur only between threads and processes running in the same virtual machine. Hence by the virtue of this boundary, snoop requests could only be limited to within this virtual machine rather sending it everywhere. However such an approach requires the support of the operating system along with hyper-visor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74101</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74101"/>
		<updated>2013-03-20T23:04:21Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Snoop Filters'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having shared memory and each processor having its own private cache is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
Snooping is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols in a bus-based multiprocessor system at each node can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block. &lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper snooped the bus transaction and and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many coherence protocols are snoop based.&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74095</id>
		<title>CSC/ECE 506 Spring 2013/8c da</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_506_Spring_2013/8c_da&amp;diff=74095"/>
		<updated>2013-03-20T22:58:59Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: Created page with &amp;quot;=Snoop Filters=  ==Introduction==  One of the issues with large systems with multiple processors having shared memory and each processor having its own private cache is the cache...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Snoop Filters=&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
One of the issues with large systems with multiple processors having shared memory and each processor having its own private cache is the cache coherence problem. The non-coherent view of values of a single data item in these different caches is referred to as the cache coherence problem. A protocol which ensures a coherent view of cached values as seen by multiple processors is referred to as cache coherence protocol.&lt;br /&gt;
&lt;br /&gt;
Snooping is the process where the individual caches monitor address lines for accesses to memory locations that they have cached. When a write operation is observed to a location that a cache has a copy of, the cache controller invalidates its own copy of the snooped memory location. Many cache coherence protocols require a hardware component to monitor the bus transactions.&lt;br /&gt;
&lt;br /&gt;
Hardware support for implementing cache coherence protocols in a bus-based multiprocessor system at each node can be provided using coherence controller. The coherence controller has a component called the snooper. The role of the snooper is to snoop each bus transaction involved in the cache coherence transaction. For each snooped bus transaction, the coherence controller checks the cache tag array to see if it has the block that is involved in the transaction, checks the current state of the block (if the block is found), and reacts accordingly by responding with data or by changing the state of the block. &lt;br /&gt;
&lt;br /&gt;
In most well-optimized programs, much data is not shared among threads. So most of the time, most snooped bus transactions do not find the block in the local cache. Even in that case, snooper snooped the bus transaction and and checked the cache tag to determine whether the cache has the block, thus incurred  unnecessary work. There is the possibility that contention can occur between processor and the snooper to access the cache tag. One possible solution to reduce contention between the processor and the snooper is to introduce a snoop filter, which determines whether a snooper needs to check the cache tag or not. By reducing the number of snooped transactions the need to check the cache tags, contention and power consumption can be reduced.&lt;br /&gt;
&lt;br /&gt;
==Why Snoop Filtering?==&lt;br /&gt;
&lt;br /&gt;
With the advent of modern computers built with multiple processing cores and shared memory programming model it has become necessary to use cache coherence protocols to maintain coherence between different caches attached to individual processing units and many coherence protocols are snoop based.&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=ECE506_Main_Page&amp;diff=74086</id>
		<title>ECE506 Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=ECE506_Main_Page&amp;diff=74086"/>
		<updated>2013-03-20T22:48:47Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page serves as a portal for all wiki material related to CSC506 and ECE506. Link to any new wiki pages from this page, and add links to any current pages.&lt;br /&gt;
&lt;br /&gt;
=Supplements to Solihin Text=&lt;br /&gt;
&lt;br /&gt;
Post links to the textbook supplements in this section.&lt;br /&gt;
*Chapter 2 [[CSC/ECE 506 Spring 2011/ch2 dm | CSC/ECE 506 Spring 2011/ch2 dm]]&lt;br /&gt;
*Chapter 2 [[Parallel_Programming_Models | Parallel Programming Models]]&lt;br /&gt;
*Chapter 2 (Still being revised) [[CSC/ECE 506 Spring 2011/ch2 cl | CSC/ECE 506 Spring 2011/ch2 cl]]&lt;br /&gt;
*Chapter 2a [[ CSC/ECE 506 Spring 2011/ch2a mc | Current Data-Parallel Architectures ]]&lt;br /&gt;
*Chapter 2a [[ CSC/ECE 506 Spring_2012/2a va ]]&lt;br /&gt;
*Chapter 2b [[CSC/ECE 506 Spring 2012/ch2b cm | CSC/ECE 506 Spring 2012/ch2b cm]]&lt;br /&gt;
*Chapter 2b [[ECE506_CSC/ECE_506_Spring_2012/2b_az | CSC/ECE 506 Spring 2012/2b az - Data-Parallel Processing with the AMD HD 6900 Series Graphics Processing Unit]]&lt;br /&gt;
*Chapter 3 (Final Revision) [[ CSC/ECE 506 Spring 2011/ch3 ab | Parallel Architecture Mechanisms and Programming Models ]]&lt;br /&gt;
*Chapter 4a[[ CSC/ECE 506 Spring 2011/ch4a ob | Parallelization of Nelder Mead Algorithm ]]&lt;br /&gt;
*Chapter 4a (Under Construction) [[ CSC/ECE_506_Spring_2011/ch4a_bm | Parallelization of Algorithms  ]]&lt;br /&gt;
*Chapter 4a [[ CSC/ECE 506 Spring 2011/ch4a zz | CSC/ECE 506 Spring 2011/ch4a zz ]]&lt;br /&gt;
*Chapter 4b [[Chapter 4b CSC/ECE 506 Spring 2011 / ch4b]]&lt;br /&gt;
*Chapter 5a [[ CSC/ECE 506 Spring 2012/ch5a ja | CSC/ECE 506 Spring 2012/ch5a ja ]]&lt;br /&gt;
*Chapter 9a [[CSC/ECE 506 Spring 2012/ch9a cm | CSC/ECE 506 Spring 2012/ch9a cm]]&lt;br /&gt;
*Chapter 6a (Under Construction) [[ CSC/ECE 506 Spring 2011/ch6a jp | CSC/ECE 506 Spring 2011/ch6a jp ]]&lt;br /&gt;
*Chapter 6a (Under Construction) [[ CSC/ECE 506 Spring 2011/ch6a ep | CSC/ECE 506 Spring 2011/ch6a ep ]]&lt;br /&gt;
*Chapter 6b (Ready for First Review) [[CSC/ECE 506 Spring 2011/ch6b ab | CSC/ECE 506 Spring 2011/ch6b ab]]&lt;br /&gt;
*Chapter 7 (Under Construction) [[CSC/ECE 506 Spring 2011/ch7 jp | CSC/ECE 506 Spring 2011/ch7 jp]]&lt;br /&gt;
*Chapter 8 [[CSC/ECE 506 Spring 2011/ch8 mc | CSC/ECE 506 Spring 2011/ch8 mc]]&lt;br /&gt;
*Chapter 10 (Under Construction) [[CSC/ECE 506 Spring 2011/ch10 sb | CSC/ECE 506 Spring 2011/ch10 sb]]&lt;br /&gt;
*Chapter 10 [[CSC/ECE 506 Spring 2012/ch10 sj | CSC/ECE 506 Spring 2012/ch10 sj]]&lt;br /&gt;
*Chapter 10a [[CSC/ECE_506_Spring_2011/ch10a_dc | CSC/ECE_506_Spring_2011/ch10a_dc]]&lt;br /&gt;
*Chapter 11 [[CSC/ECE_506_Spring_2011/ch11_BB_EP | Chapter 11 Supplement]]&lt;br /&gt;
*Chapter 11 [[Scalable_Coherent_Interface | SCI (Scalable Coherent Interface) ]]&lt;br /&gt;
*Chapter 12 [[ CSC/ECE 506 Spring 2011/ch12 ob | Interconnection Network Topologies and Routing Algorithms]]&lt;br /&gt;
*Chapter 12 (Ready for Final Review) [[ CSC/ECE 506 Spring 2011/ch12 aj | Interconnection Network Topologies and Routing Algorithms]]&lt;br /&gt;
*Chapter 12 [[ CSC/ECE 506 Spring 2011/ch12 | Interconnection Network Topologies]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/1a ry]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/1c dm]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/1c cl]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/1a mw]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/3a yw]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/7b yw]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/3b sk]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/4b rs]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/6b am]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/8a cj]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/10a dr]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/10a jp]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/9a ms]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/10b sr]]&lt;br /&gt;
*Chapter 11a [[ECE506_CSC/ECE_506_Spring_2012/11a_az | CSC/ECE 506 Spring 2012/11a az - Performance of DSM system]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/12b jh]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2010/8a fu]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2010/8a sk]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/11a ht]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/1b dj]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/1a sp]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/1d ks]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/2b so]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/1c ad]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/3b xz]]&lt;br /&gt;
*[[CSC/ECE_506_Spring_2013/4a_aj]]&lt;br /&gt;
*[[CSC/ECE_506_Spring_2013/4a_ss]]&lt;br /&gt;
*[[CSC/ECE_506_Spring_2013/1a_ag]]&lt;br /&gt;
* Chapter 3a [[CSC/ECE_506_Spring_2013/3a_bs]]&lt;br /&gt;
* Chapter 6a [[CSC/ECE_506_Spring_2013/6a_cs]]&lt;br /&gt;
* Chapter 5a [[CSC/ECE_506_Spring_2013/5a_ks]]&lt;br /&gt;
* Chapter 8a [[CSC/ECE_506_Spring_2013/8a_an]]&lt;br /&gt;
* Chapter 7a [[CSC/ECE_506_Spring_2013/7a_bs]]&lt;br /&gt;
* Chapter 8b [[CSC/ECE_506_Spring_2013/8b_ap]]&lt;br /&gt;
* Chpater 8c [[CSC/ECE_506_Spring_2013/8c_da]]&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=ECE506_Main_Page&amp;diff=74081</id>
		<title>ECE506 Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=ECE506_Main_Page&amp;diff=74081"/>
		<updated>2013-03-20T22:45:11Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page serves as a portal for all wiki material related to CSC506 and ECE506. Link to any new wiki pages from this page, and add links to any current pages.&lt;br /&gt;
&lt;br /&gt;
=Supplements to Solihin Text=&lt;br /&gt;
&lt;br /&gt;
Post links to the textbook supplements in this section.&lt;br /&gt;
*Chapter 2 [[CSC/ECE 506 Spring 2011/ch2 dm | CSC/ECE 506 Spring 2011/ch2 dm]]&lt;br /&gt;
*Chapter 2 [[Parallel_Programming_Models | Parallel Programming Models]]&lt;br /&gt;
*Chapter 2 (Still being revised) [[CSC/ECE 506 Spring 2011/ch2 cl | CSC/ECE 506 Spring 2011/ch2 cl]]&lt;br /&gt;
*Chapter 2a [[ CSC/ECE 506 Spring 2011/ch2a mc | Current Data-Parallel Architectures ]]&lt;br /&gt;
*Chapter 2a [[ CSC/ECE 506 Spring_2012/2a va ]]&lt;br /&gt;
*Chapter 2b [[CSC/ECE 506 Spring 2012/ch2b cm | CSC/ECE 506 Spring 2012/ch2b cm]]&lt;br /&gt;
*Chapter 2b [[ECE506_CSC/ECE_506_Spring_2012/2b_az | CSC/ECE 506 Spring 2012/2b az - Data-Parallel Processing with the AMD HD 6900 Series Graphics Processing Unit]]&lt;br /&gt;
*Chapter 3 (Final Revision) [[ CSC/ECE 506 Spring 2011/ch3 ab | Parallel Architecture Mechanisms and Programming Models ]]&lt;br /&gt;
*Chapter 4a[[ CSC/ECE 506 Spring 2011/ch4a ob | Parallelization of Nelder Mead Algorithm ]]&lt;br /&gt;
*Chapter 4a (Under Construction) [[ CSC/ECE_506_Spring_2011/ch4a_bm | Parallelization of Algorithms  ]]&lt;br /&gt;
*Chapter 4a [[ CSC/ECE 506 Spring 2011/ch4a zz | CSC/ECE 506 Spring 2011/ch4a zz ]]&lt;br /&gt;
*Chapter 4b [[Chapter 4b CSC/ECE 506 Spring 2011 / ch4b]]&lt;br /&gt;
*Chapter 5a [[ CSC/ECE 506 Spring 2012/ch5a ja | CSC/ECE 506 Spring 2012/ch5a ja ]]&lt;br /&gt;
*Chapter 9a [[CSC/ECE 506 Spring 2012/ch9a cm | CSC/ECE 506 Spring 2012/ch9a cm]]&lt;br /&gt;
*Chapter 6a (Under Construction) [[ CSC/ECE 506 Spring 2011/ch6a jp | CSC/ECE 506 Spring 2011/ch6a jp ]]&lt;br /&gt;
*Chapter 6a (Under Construction) [[ CSC/ECE 506 Spring 2011/ch6a ep | CSC/ECE 506 Spring 2011/ch6a ep ]]&lt;br /&gt;
*Chapter 6b (Ready for First Review) [[CSC/ECE 506 Spring 2011/ch6b ab | CSC/ECE 506 Spring 2011/ch6b ab]]&lt;br /&gt;
*Chapter 7 (Under Construction) [[CSC/ECE 506 Spring 2011/ch7 jp | CSC/ECE 506 Spring 2011/ch7 jp]]&lt;br /&gt;
*Chapter 8 [[CSC/ECE 506 Spring 2011/ch8 mc | CSC/ECE 506 Spring 2011/ch8 mc]]&lt;br /&gt;
*Chapter 10 (Under Construction) [[CSC/ECE 506 Spring 2011/ch10 sb | CSC/ECE 506 Spring 2011/ch10 sb]]&lt;br /&gt;
*Chapter 10 [[CSC/ECE 506 Spring 2012/ch10 sj | CSC/ECE 506 Spring 2012/ch10 sj]]&lt;br /&gt;
*Chapter 10a [[CSC/ECE_506_Spring_2011/ch10a_dc | CSC/ECE_506_Spring_2011/ch10a_dc]]&lt;br /&gt;
*Chapter 11 [[CSC/ECE_506_Spring_2011/ch11_BB_EP | Chapter 11 Supplement]]&lt;br /&gt;
*Chapter 11 [[Scalable_Coherent_Interface | SCI (Scalable Coherent Interface) ]]&lt;br /&gt;
*Chapter 12 [[ CSC/ECE 506 Spring 2011/ch12 ob | Interconnection Network Topologies and Routing Algorithms]]&lt;br /&gt;
*Chapter 12 (Ready for Final Review) [[ CSC/ECE 506 Spring 2011/ch12 aj | Interconnection Network Topologies and Routing Algorithms]]&lt;br /&gt;
*Chapter 12 [[ CSC/ECE 506 Spring 2011/ch12 | Interconnection Network Topologies]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/1a ry]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/1c dm]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/1c cl]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/1a mw]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/3a yw]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/7b yw]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/3b sk]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/4b rs]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/6b am]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/8a cj]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/10a dr]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/10a jp]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/9a ms]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/10b sr]]&lt;br /&gt;
*Chapter 11a [[ECE506_CSC/ECE_506_Spring_2012/11a_az | CSC/ECE 506 Spring 2012/11a az - Performance of DSM system]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/12b jh]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2010/8a fu]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2010/8a sk]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2012/11a ht]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/1b dj]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/1a sp]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/1d ks]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/2b so]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/1c ad]]&lt;br /&gt;
*[[CSC/ECE 506 Spring 2013/3b xz]]&lt;br /&gt;
*[[CSC/ECE_506_Spring_2013/4a_aj]]&lt;br /&gt;
*[[CSC/ECE_506_Spring_2013/4a_ss]]&lt;br /&gt;
*[[CSC/ECE_506_Spring_2013/1a_ag]]&lt;br /&gt;
* Chapter 3a [[CSC/ECE_506_Spring_2013/3a_bs]]&lt;br /&gt;
* Chapter 6a [[CSC/ECE_506_Spring_2013/6a_cs]]&lt;br /&gt;
* Chapter 5a [[CSC/ECE_506_Spring_2013/5a_ks]]&lt;br /&gt;
* Chapter 8a [[CSC/ECE_506_Spring_2013/8a_an]]&lt;br /&gt;
* Chapter 7a [[CSC/ECE_506_Spring_2013/7a_bs]]&lt;br /&gt;
* Chapter 8b [[CSC/ECE_506_Spring_2013/8b_ap]]&lt;br /&gt;
* Chpater 8c [[CSC/ECE_506_Spring_2013/8b_da]]&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70302</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w28 dh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70302"/>
		<updated>2012-11-18T19:40:05Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''SaaS - 4.8 - 4.9 - Fallacies &amp;amp; pitfalls, BDD pros &amp;amp; cons'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to Software Engineering Fallacies and Pitfalls==&lt;br /&gt;
&lt;br /&gt;
In the history of the software field, a lot of facts have been identified, and a lot of [http://en.wikipedia.org/wiki/List_of_fallacies fallacies] promulgated. There’s a problem with those facts–and, as you might imagine, those fallacies. Many of these fundamentally important facts are learned by a [http://en.wikipedia.org/wiki/Software_engineer software engineer], but over the lifespan of the software field, all too many of them have been forgotten. These facts and fallacies are fundamental to the software building field–forget or neglect them at your peril!&lt;br /&gt;
&lt;br /&gt;
==Fallacies==&lt;br /&gt;
&lt;br /&gt;
Five Fatal Logical Fallacies of Software Development&lt;br /&gt;
&lt;br /&gt;
===Fallacy Fallacy===&lt;br /&gt;
&lt;br /&gt;
Drawing the conclusion that another engineer's point is incorrect because he cannot adequately defend his position. &lt;br /&gt;
Engineers often have an intuition about engineering solutions — an intuition that isn't always easily quantifiable into a well formed argument. Engineer should seek first to understand the possible reasons for taking an opposing position so that, ultimately, his/her designs will be better informed. Moreover, if that engineer is in a decision making position, his/her decisions will be viewed as more fair and well thought out. &lt;br /&gt;
&lt;br /&gt;
===Appeal to Tradition===&lt;br /&gt;
&lt;br /&gt;
Justifying or defending a coding practice by citing precedent.&lt;br /&gt;
It is usually an internal struggle between introducing newer, more well thought out designs vs adopting a 'When in Rome do as the Romans do' strategy to maintain consistency in the code base.&lt;br /&gt;
Processes, procedures and coding practices aren't developed in a vacuum. There are a dizzying array of external factors that influence these decisions. Many times, if one examines the factors that led to these decisions, he/she will find that the original reasons no longer apply or are diminished. It never hurts to apply the five whys to find out if it makes sense to continue a particular practice.&lt;br /&gt;
&lt;br /&gt;
===The Sunk Cost Fallacy===&lt;br /&gt;
&lt;br /&gt;
Believing to continue forward with a particular design, project, etc. because too much time, money and other resources have already been poured into a solution.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Software_engineering Software engineering] is hard. When a project is started,  all of the facts are not present. There should not be any fear to change direction once new information leads to question the viability of the original decisions.&lt;br /&gt;
Is that [http://en.wikipedia.org/wiki/Refactoring refactoring] effort that you thought would take 2 days instead proving to drag into 2 weeks with no end in sight? Don't be afraid to stop because now it is known that its more involved that it was originally thought. Cut your losses. It will make the project deadline and the [http://en.wikipedia.org/wiki/Stakeholder stakeholders] happy.&lt;br /&gt;
&lt;br /&gt;
===The False Dichotomy===&lt;br /&gt;
&lt;br /&gt;
Constraining a problem space to two — and only two solutions.&lt;br /&gt;
Statements like the following are often made: 'The slow performance is due to either a missing database index or a changed execution plan from crossing a partition'.&lt;br /&gt;
While these may be likely culprits, such statements ignore an entire universe of other possibilities including [http://en.wikipedia.org/wiki/Java_virtual_machine JVM] [http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 GC] pauses, [http://en.wikipedia.org/wiki/Network_latency#Packet-switched_networks network latencies], etc. Rarely are there only two possibilities when dealing with undiagnosed problems.&lt;br /&gt;
&lt;br /&gt;
===Confusing Correlation and Causation===&lt;br /&gt;
&lt;br /&gt;
Assuming that because events occur together that one must cause the other.&lt;br /&gt;
This is another fallacy that tends to be seen when [http://en.wikipedia.org/wiki/Troubleshooting troubleshooting] production issues. In such situations, reams of data are analyzed and we humans do what we're best at: [http://en.wikipedia.org/wiki/Pattern_recognition pattern recognition]. If we're not careful, this pattern recognition can lead to assume that one event causes another to occur.&lt;br /&gt;
Incorrectly asserting causation is dangerous in this case because it can lead down to unproductive exploratory paths and thus lead to longer diagnosis times. Knowing in advance that events are correlated but not necessarily causal can introduce a healthy dose of skepticism into the troubleshooting process.&lt;br /&gt;
&lt;br /&gt;
==Pitfalls==&lt;br /&gt;
&lt;br /&gt;
===Temptation to Add Cool Features===&lt;br /&gt;
&lt;br /&gt;
Adding cool features that do not make the product more successful. Programmers are usually tempted to add cool features that customers don’t like. It is called pitfall because it happens even when programmer knows it. Programmer knows it will be rejected but he/she does it anyways and thinks that the [http://en.wikipedia.org/wiki/Customer customer] will like it. But the customers hate it and it gets rejected.&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Philosophy philosophy] is to use a method such as [http://en.wikipedia.org/wiki/User_stories user stories] to help prioritize the tasks that are needed to be done first. Then if there is time for cool features, they can be added. It helps in reducing wasted effort on the features that are not at all required by the customers.&lt;br /&gt;
&lt;br /&gt;
===Predicting Code===&lt;br /&gt;
&lt;br /&gt;
Trying to predict what code you need before need it&lt;br /&gt;
Programmers sometimes love to predict what customer wants and go ahead and put those features in the code. But when programmers show the feature to the customer, they disapprove it.&lt;br /&gt;
Hence there is a need to write the test first, sync with the customer and then implement it. Programmers can use Behavior-Driven-Design (BDD) to write tests before code they need, then write code to pass the tests. In this case there is no need to predict and no wastage of development time and resources.&lt;br /&gt;
&lt;br /&gt;
===Negative Expectations===&lt;br /&gt;
&lt;br /&gt;
Careless use of negative expectations&lt;br /&gt;
While writing tests first, the more detailed thing is when programmers say what is not supposed to happen. But the downside is there are millions of things that are not supposed to happen, so it’s pretty easy to pass the things that are not happening. But one probably do not want to do that. &lt;br /&gt;
Many, many outputs are incorrect. The programmers should be beware of overusing “Then I should not see...”. They cannot tell if output is what they want, by saying only that it is not what they want. Hence it is better to include positives to check results such as “Then I should see...”. It will clearly state what is expected by the program.&lt;br /&gt;
&lt;br /&gt;
===Why Do Softwares Fail===&lt;br /&gt;
&lt;br /&gt;
There is a sad line about software before the agile came into software development field that ‘If we built all the building like we build software, the first woodpecker would destroy civilization.’ So what was the reason behind this saying? Following are the problems with the method of software development before agile and BDD:&lt;br /&gt;
&lt;br /&gt;
===Softwares don’t do what Customers want===&lt;br /&gt;
&lt;br /&gt;
After a software is built and presented to the customer, they realize that “That’s not what they want, even though it sounded like it.” &lt;br /&gt;
&lt;br /&gt;
===Projects are Late===&lt;br /&gt;
&lt;br /&gt;
The problem while making a software project is the estimation of the time to build it. It is very common for the software engineering projects to run out of time. Hence they are delivered late than the date estimated.&lt;br /&gt;
&lt;br /&gt;
===Over Budget===&lt;br /&gt;
&lt;br /&gt;
Another common problem with the software development project is the estimation of the budget. Most of the projects run out of money before they are completed.&lt;br /&gt;
&lt;br /&gt;
===Hard to Maintain and Evolve===&lt;br /&gt;
&lt;br /&gt;
People expect software today to be maintainable or evolve whenever necessary. Sometimes the software project is built perfectly fine for the first time and works as intended. But it is really hard to maintain and evolve later.&lt;br /&gt;
&lt;br /&gt;
This inspired the Agile Manifesto. The solution that Agile methodology provides is to work closely and continuously with the customer, not like sign a contract and come back in two years and show them the results. Not only the customer but all the stakeholders, the end users of the system, developers, people maintaining the code should be involved, so that all are on the same page. Behavior-Driven Design is the design method that gives an idea to capture the behavior that is required as opposed to how it is implemented.&lt;br /&gt;
&lt;br /&gt;
==Behaviour-Driven Design (BDD)==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
Behavior-driven design (BDD) is a software development process which combines the principles of test-driven development and object-oriented analysis and design to provide business analysts and software developers with a process to develop quality software. &lt;br /&gt;
&lt;br /&gt;
A specialized version of Test-driven development which focuses on the behavior of the application being developed is the core of Behavior Driven Development, in which software developers must define a test set for each software unit, make sure the tests fail before implementation of the unit, implement the unit and verify that tests succeed once the unit has been implemented. &lt;br /&gt;
&lt;br /&gt;
BDD asks questions about the behavior of the application being developed before and during development to reduce miscommunication. In this process the requirements are written down as user stories in the form of “3x5” cards, and all the stakeholders can mention their requirements without hesitation. These user stories are used to create acceptance tests before the actual code is written.&lt;br /&gt;
&lt;br /&gt;
[[File:BDD_life_cycle.jpg|thumb|right|alt= Behavior-Driven Development Life Cycle]]&lt;br /&gt;
&lt;br /&gt;
===Pros of using BDD===&lt;br /&gt;
&lt;br /&gt;
The following are some of the advantages of using Behavior-Driven Design:&lt;br /&gt;
&lt;br /&gt;
* Makes it Easy to Work with Customers.&lt;br /&gt;
&lt;br /&gt;
BDD uses user stories which are non-technical and allows all stakeholders to easily understand and state the requirements. Storyboards and UI sketches are used in BDD to allow all stakeholders including nontechnical to contribute towards requirement specification. Using “3x5” user story cards customers can state their requirements more effectively and without the trouble of using technical terminology. &lt;br /&gt;
&lt;br /&gt;
* Enables customer to drive the end product development&lt;br /&gt;
&lt;br /&gt;
Customers are continuously involved in the development of the software so that they can drive (and effectively change) the behaviour of the end product during the course of the development.&lt;br /&gt;
&lt;br /&gt;
* Tests are written before debugging&lt;br /&gt;
&lt;br /&gt;
BDD helps software developers to come up with tests before debugging issues with the software and even before writing the code. This allows the developers to follow the Test-driven software development methodology.&lt;br /&gt;
&lt;br /&gt;
* User stories can be formulated as acceptance tests before code is written&lt;br /&gt;
&lt;br /&gt;
The user stories written by various stakeholders can be directly used to formulate acceptance test cases. So developers have acceptance test cases available even before the code development starts. This can be achieved through the tools such as cucumber in the Rails development framework which takes user stories as input to create acceptance test cases.&lt;br /&gt;
&lt;br /&gt;
* Easy to change requirements during development process&lt;br /&gt;
&lt;br /&gt;
The customer requirements and expectations may evolve and change during the course of software development. BDD has the capability to incorporate new insights from customers during development. This mainly makes use of “3x5” index cards, since the requirements written on these cards are short it is easy to change them during development.&lt;br /&gt;
&lt;br /&gt;
* Customer can keep track of the progress in development through iterations&lt;br /&gt;
&lt;br /&gt;
Software developers get to meet with respective stakeholders once a week and show them how various iterations of the product being developed work.&lt;br /&gt;
&lt;br /&gt;
===Cons of using BDD===&lt;br /&gt;
&lt;br /&gt;
* Customer may not be interested to make continuous contact with developers.&lt;br /&gt;
&lt;br /&gt;
Behavior-driven software development may not be very effective if the customer refuses to make continuous contact with the developer and review the various iterations. The success of BDD depends on continuous interaction with customer so as to shape the end product as per their expectations, therefore it can be disadvantageous to the project if the customer is reluctant to make continuous interactions during product development.&lt;br /&gt;
&lt;br /&gt;
* Leads to bad software architecture&lt;br /&gt;
&lt;br /&gt;
Customer driven software development can sometimes lead to bad software architecture because of continuously evolving customer requirements which then reflects in the software architecture. Software which lacks good architecture makes it hard for developers to maintain the software and add new features to the existing version.&lt;br /&gt;
&lt;br /&gt;
===“Debugging Sucks, Testing Rocks”===&lt;br /&gt;
&lt;br /&gt;
This is a philosophy which suggests that tests for softwares should be written in advance so that the developer does not spend all his time debugging various issues in the software. According to this philosophy it is better to invest more time into testing than into debugging, as this approach in turn considerably reduces the time spent on debugging issues.&lt;br /&gt;
&lt;br /&gt;
[[File:DS_TR.jpg|thumb|right|alt=“Debugging Sucks, Testing Rocks”]]&lt;br /&gt;
&lt;br /&gt;
===How Rails tools make it easier to follow BDD===&lt;br /&gt;
&lt;br /&gt;
BDD can be made easier using various tools in the Ruby on Rails web development framework. One such tool which supports BDD is Cucumber. Cucumber tests the software for customer understandable user stories. Cucumber can be used to ensure that customer accepts the final product and various interfaces between modules communicate efficiently. So in general Cucumber meets halfway between customer and developer. &lt;br /&gt;
&lt;br /&gt;
'''Example'''&lt;br /&gt;
&lt;br /&gt;
The following example shows how Cucumber can be used in BDD:&lt;br /&gt;
&lt;br /&gt;
1. Describe behaviour in plain text:&lt;br /&gt;
&lt;br /&gt;
 '''Feature:''' Addition&lt;br /&gt;
     '''In order''' to avoid silly mistakes&lt;br /&gt;
     '''As an''' amateur in math&lt;br /&gt;
     '''I want''' to be told the sum of two numbers&lt;br /&gt;
&lt;br /&gt;
 '''Scenario:''' Add two numbers&lt;br /&gt;
     '''Given''' I have entered 50 into the calculator&lt;br /&gt;
     '''And''' I have entered 70 into the calculator&lt;br /&gt;
     '''When''' I press add&lt;br /&gt;
     '''Then''' the result should be 120 on the screen &lt;br /&gt;
&lt;br /&gt;
2. Write a step definition in Ruby&lt;br /&gt;
&lt;br /&gt;
 '''Given /I''' have entered ('''.*''') into the calculator'''/ do''' |n|&lt;br /&gt;
    calculator = Calculator.'''new'''&lt;br /&gt;
    calculator.push(n.to_i)&lt;br /&gt;
 '''end'''&lt;br /&gt;
&lt;br /&gt;
3. Run and watch the test fail. The failed test will appear as red in Cucumber.&lt;br /&gt;
&lt;br /&gt;
4. Write code to make the step pass.&lt;br /&gt;
&lt;br /&gt;
5. Run and see the step pass. Once a step passes it is displayed in green.&lt;br /&gt;
&lt;br /&gt;
6. Repeat steps 2 to 5 until all tests become green.&lt;br /&gt;
&lt;br /&gt;
===How is BDD and TDD helpful in industry===&lt;br /&gt;
&lt;br /&gt;
BDD and TDD are widely used in the industry for customer driven software development following the agile software development methodology. Many recently graduated professionals in the industry agree that BDD and TDD are greatly helpful in developing quality software in a short period of time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70293</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w28 dh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70293"/>
		<updated>2012-11-18T19:34:50Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''SaaS - 4.8 - 4.9 - Fallacies &amp;amp; pitfalls, BDD pros &amp;amp; cons'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to Software Engineering Fallacies and Pitfalls==&lt;br /&gt;
&lt;br /&gt;
In the history of the software field, a lot of facts have been identified, and a lot of [http://en.wikipedia.org/wiki/List_of_fallacies fallacies] promulgated. There’s a problem with those facts–and, as you might imagine, those fallacies. Many of these fundamentally important facts are learned by a [http://en.wikipedia.org/wiki/Software_engineer software engineer], but over the lifespan of the software field, all too many of them have been forgotten. These facts and fallacies are fundamental to the software building field–forget or neglect them at your peril!&lt;br /&gt;
&lt;br /&gt;
==Fallacies==&lt;br /&gt;
&lt;br /&gt;
Five Fatal Logical Fallacies of Software Development&lt;br /&gt;
&lt;br /&gt;
===Fallacy Fallacy===&lt;br /&gt;
&lt;br /&gt;
Drawing the conclusion that another engineer's point is incorrect because he cannot adequately defend his position. &lt;br /&gt;
Engineers often have an intuition about engineering solutions — an intuition that isn't always easily quantifiable into a well formed argument. Engineer should seek first to understand the possible reasons for taking an opposing position so that, ultimately, his/her designs will be better informed. Moreover, if that engineer is in a decision making position, his/her decisions will be viewed as more fair and well thought out. &lt;br /&gt;
&lt;br /&gt;
===Appeal to Tradition===&lt;br /&gt;
&lt;br /&gt;
Justifying or defending a coding practice by citing precedent.&lt;br /&gt;
It is usually an internal struggle between introducing newer, more well thought out designs vs adopting a 'When in Rome do as the Romans do' strategy to maintain consistency in the code base.&lt;br /&gt;
Processes, procedures and coding practices aren't developed in a vacuum. There are a dizzying array of external factors that influence these decisions. Many times, if one examines the factors that led to these decisions, he/she will find that the original reasons no longer apply or are diminished. It never hurts to apply the five whys to find out if it makes sense to continue a particular practice.&lt;br /&gt;
&lt;br /&gt;
===The Sunk Cost Fallacy===&lt;br /&gt;
&lt;br /&gt;
Believing to continue forward with a particular design, project, etc. because too much time, money and other resources have already been poured into a solution.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Software_engineering Software engineering] is hard. When a project is started,  all of the facts are not present. There should not be any fear to change direction once new information leads to question the viability of the original decisions.&lt;br /&gt;
Is that [http://en.wikipedia.org/wiki/Refactoring refactoring] effort that you thought would take 2 days instead proving to drag into 2 weeks with no end in sight? Don't be afraid to stop because now it is known that its more involved that it was originally thought. Cut your losses. It will make the project deadline and the [http://en.wikipedia.org/wiki/Stakeholder stakeholders] happy.&lt;br /&gt;
&lt;br /&gt;
===The False Dichotomy===&lt;br /&gt;
&lt;br /&gt;
Constraining a problem space to two — and only two solutions.&lt;br /&gt;
Statements like the following are often made: 'The slow performance is due to either a missing database index or a changed execution plan from crossing a partition'.&lt;br /&gt;
While these may be likely culprits, such statements ignore an entire universe of other possibilities including [http://en.wikipedia.org/wiki/Java_virtual_machine JVM] [http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 GC] pauses, [http://en.wikipedia.org/wiki/Network_latency#Packet-switched_networks network latencies], etc. Rarely are there only two possibilities when dealing with undiagnosed problems.&lt;br /&gt;
&lt;br /&gt;
===Confusing Correlation and Causation===&lt;br /&gt;
&lt;br /&gt;
Assuming that because events occur together that one must cause the other.&lt;br /&gt;
This is another fallacy that tends to be seen when troubleshooting production issues. In such situations, reams of data are analyzed and we humans do what we're best at: pattern recognition. If we're not careful, this pattern recognition can lead to assume that one event causes another to occur.&lt;br /&gt;
Incorrectly asserting causation is dangerous in this case because it can lead down to unproductive exploratory paths and thus lead to longer diagnosis times. Knowing in advance that events are correlated but not necessarily causal can introduce a healthy dose of skepticism into the troubleshooting process.&lt;br /&gt;
&lt;br /&gt;
==Pitfalls==&lt;br /&gt;
&lt;br /&gt;
===Temptation to Add Cool Features===&lt;br /&gt;
&lt;br /&gt;
Adding cool features that do not make the product more successful. Programmers are usually tempted to add cool features that customers don’t like. It is called pitfall because it happens even when programmer knows it. Programmer knows it will be rejected but he/she does it anyways and thinks that the customer will like it. But the customers hate it and it gets rejected.&lt;br /&gt;
The philosophy is use a method such as user stories to help prioritize the tasks that are needed to be done first. Then if there is time for cool features, they can be added. It helps in reducing wasted effort on the features that are not at all required by the customers.&lt;br /&gt;
&lt;br /&gt;
===Predicting Code===&lt;br /&gt;
&lt;br /&gt;
Trying to predict what code you need before need it&lt;br /&gt;
Programmers sometimes love to predict what customer wants and go ahead and put those features in the code. But when programmers show the feature to the customer, they disapprove it.&lt;br /&gt;
Hence there is a need to write the test first, sync with the customer and then implement it. Programmers can use Behavior-Driven-Design (BDD) to write tests before code they need, then write code to pass the tests. In this case there is no need to predict and no wastage of development time and resources.&lt;br /&gt;
&lt;br /&gt;
===Negative Expectations===&lt;br /&gt;
&lt;br /&gt;
Careless use of negative expectations&lt;br /&gt;
While writing tests first, the more detailed thing is when programmers say what is not supposed to happen. But the downside is there are millions of things that are not supposed to happen, so it’s pretty easy to pass the things that are not happening. But one probably do not want to do that. &lt;br /&gt;
Many, many outputs are incorrect. The programmers should be beware of overusing “Then I should not see...”. They cannot tell if output is what they want, by saying only that it is not what they want. Hence it is better to include positives to check results such as “Then I should see...”. It will clearly state what is expected by the program.&lt;br /&gt;
&lt;br /&gt;
===Why Do Softwares Fail===&lt;br /&gt;
&lt;br /&gt;
There is a sad line about software before the agile came into software development field that ‘If we built all the building like we build software, the first woodpecker would destroy civilization.’ So what was the reason behind this saying? Following are the problems with the method of software development before agile and BDD:&lt;br /&gt;
&lt;br /&gt;
===Softwares don’t do what Customers want===&lt;br /&gt;
&lt;br /&gt;
After a software is built and presented to the customer, they realize that “That’s not what they want, even though it sounded like it.” &lt;br /&gt;
&lt;br /&gt;
===Projects are Late===&lt;br /&gt;
&lt;br /&gt;
The problem while making a software project is the estimation of the time to build it. It is very common for the software engineering projects to run out of time. Hence they are delivered late than the date estimated.&lt;br /&gt;
&lt;br /&gt;
===Over Budget===&lt;br /&gt;
&lt;br /&gt;
Another common problem with the software development project is the estimation of the budget. Most of the projects run out of money before they are completed.&lt;br /&gt;
&lt;br /&gt;
===Hard to Maintain and Evolve===&lt;br /&gt;
&lt;br /&gt;
People expect software today to be maintainable or evolve whenever necessary. Sometimes the software project is built perfectly fine for the first time and works as intended. But it is really hard to maintain and evolve later.&lt;br /&gt;
&lt;br /&gt;
This inspired the Agile Manifesto. The solution that Agile methodology provides is to work closely and continuously with the customer, not like sign a contract and come back in two years and show them the results. Not only the customer but all the stakeholders, the end users of the system, developers, people maintaining the code should be involved, so that all are on the same page. Behavior-Driven Design is the design method that gives an idea to capture the behavior that is required as opposed to how it is implemented.&lt;br /&gt;
&lt;br /&gt;
==Behaviour-Driven Design (BDD)==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
Behavior-driven design (BDD) is a software development process which combines the principles of test-driven development and object-oriented analysis and design to provide business analysts and software developers with a process to develop quality software. &lt;br /&gt;
&lt;br /&gt;
A specialized version of Test-driven development which focuses on the behavior of the application being developed is the core of Behavior Driven Development, in which software developers must define a test set for each software unit, make sure the tests fail before implementation of the unit, implement the unit and verify that tests succeed once the unit has been implemented. &lt;br /&gt;
&lt;br /&gt;
BDD asks questions about the behavior of the application being developed before and during development to reduce miscommunication. In this process the requirements are written down as user stories in the form of “3x5” cards, and all the stakeholders can mention their requirements without hesitation. These user stories are used to create acceptance tests before the actual code is written.&lt;br /&gt;
&lt;br /&gt;
[[File:BDD_life_cycle.jpg|thumb|right|alt= Behavior-Driven Development Life Cycle]]&lt;br /&gt;
&lt;br /&gt;
===Pros of using BDD===&lt;br /&gt;
&lt;br /&gt;
The following are some of the advantages of using Behavior-Driven Design:&lt;br /&gt;
&lt;br /&gt;
* Makes it Easy to Work with Customers.&lt;br /&gt;
&lt;br /&gt;
BDD uses user stories which are non-technical and allows all stakeholders to easily understand and state the requirements. Storyboards and UI sketches are used in BDD to allow all stakeholders including nontechnical to contribute towards requirement specification. Using “3x5” user story cards customers can state their requirements more effectively and without the trouble of using technical terminology. &lt;br /&gt;
&lt;br /&gt;
* Enables customer to drive the end product development&lt;br /&gt;
&lt;br /&gt;
Customers are continuously involved in the development of the software so that they can drive (and effectively change) the behaviour of the end product during the course of the development.&lt;br /&gt;
&lt;br /&gt;
* Tests are written before debugging&lt;br /&gt;
&lt;br /&gt;
BDD helps software developers to come up with tests before debugging issues with the software and even before writing the code. This allows the developers to follow the Test-driven software development methodology.&lt;br /&gt;
&lt;br /&gt;
* User stories can be formulated as acceptance tests before code is written&lt;br /&gt;
&lt;br /&gt;
The user stories written by various stakeholders can be directly used to formulate acceptance test cases. So developers have acceptance test cases available even before the code development starts. This can be achieved through the tools such as cucumber in the Rails development framework which takes user stories as input to create acceptance test cases.&lt;br /&gt;
&lt;br /&gt;
* Easy to change requirements during development process&lt;br /&gt;
&lt;br /&gt;
The customer requirements and expectations may evolve and change during the course of software development. BDD has the capability to incorporate new insights from customers during development. This mainly makes use of “3x5” index cards, since the requirements written on these cards are short it is easy to change them during development.&lt;br /&gt;
&lt;br /&gt;
* Customer can keep track of the progress in development through iterations&lt;br /&gt;
&lt;br /&gt;
Software developers get to meet with respective stakeholders once a week and show them how various iterations of the product being developed work.&lt;br /&gt;
&lt;br /&gt;
===Cons of using BDD===&lt;br /&gt;
&lt;br /&gt;
* Customer may not be interested to make continuous contact with developers.&lt;br /&gt;
&lt;br /&gt;
Behavior-driven software development may not be very effective if the customer refuses to make continuous contact with the developer and review the various iterations. The success of BDD depends on continuous interaction with customer so as to shape the end product as per their expectations, therefore it can be disadvantageous to the project if the customer is reluctant to make continuous interactions during product development.&lt;br /&gt;
&lt;br /&gt;
* Leads to bad software architecture&lt;br /&gt;
&lt;br /&gt;
Customer driven software development can sometimes lead to bad software architecture because of continuously evolving customer requirements which then reflects in the software architecture. Software which lacks good architecture makes it hard for developers to maintain the software and add new features to the existing version.&lt;br /&gt;
&lt;br /&gt;
===“Debugging Sucks, Testing Rocks”===&lt;br /&gt;
&lt;br /&gt;
This is a philosophy which suggests that tests for softwares should be written in advance so that the developer does not spend all his time debugging various issues in the software. According to this philosophy it is better to invest more time into testing than into debugging, as this approach in turn considerably reduces the time spent on debugging issues.&lt;br /&gt;
&lt;br /&gt;
[[File:DS_TR.jpg|thumb|right|alt=“Debugging Sucks, Testing Rocks”]]&lt;br /&gt;
&lt;br /&gt;
===How Rails tools make it easier to follow BDD===&lt;br /&gt;
&lt;br /&gt;
BDD can be made easier using various tools in the Ruby on Rails web development framework. One such tool which supports BDD is Cucumber. Cucumber tests the software for customer understandable user stories. Cucumber can be used to ensure that customer accepts the final product and various interfaces between modules communicate efficiently. So in general Cucumber meets halfway between customer and developer. &lt;br /&gt;
&lt;br /&gt;
'''Example'''&lt;br /&gt;
&lt;br /&gt;
The following example shows how Cucumber can be used in BDD:&lt;br /&gt;
&lt;br /&gt;
#Describe behaviour in plain text:&lt;br /&gt;
&lt;br /&gt;
 '''Feature:''' Addition&lt;br /&gt;
	'''In order''' to avoid silly mistakes&lt;br /&gt;
	'''As an''' amateur in math&lt;br /&gt;
	'''I want''' to be told the sum of two numbers&lt;br /&gt;
&lt;br /&gt;
 '''Scenario:''' Add two numbers&lt;br /&gt;
	'''Given''' I have entered 50 into the calculator&lt;br /&gt;
	'''And''' I have entered 70 into the calculator&lt;br /&gt;
	'''When''' I press add&lt;br /&gt;
	'''Then''' the result should be 120 on the screen &lt;br /&gt;
&lt;br /&gt;
#Write a step definition in Ruby&lt;br /&gt;
&lt;br /&gt;
 '''Given /I''' have entered ('''.*''') into the calculator'''/ do''' |n|&lt;br /&gt;
	calculator = Calculator.'''new'''&lt;br /&gt;
	calculator.push(n.to_i)&lt;br /&gt;
 '''end'''&lt;br /&gt;
&lt;br /&gt;
#Run and watch the test fail. The failed test will appear as red in Cucumber.&lt;br /&gt;
&lt;br /&gt;
#Write code to make the step pass.&lt;br /&gt;
&lt;br /&gt;
#Run and see the step pass. Once a step passes it is displayed in green.&lt;br /&gt;
&lt;br /&gt;
#Repeat steps 2 to 5 until all tests become green.&lt;br /&gt;
&lt;br /&gt;
===How is BDD and TDD helpful in industry===&lt;br /&gt;
&lt;br /&gt;
BDD and TDD are widely used in the industry for customer driven software development following the agile software development methodology. Many recently graduated professionals in the industry agree that BDD and TDD are greatly helpful in developing quality software in a short period of time.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:DS_TR.jpg&amp;diff=70283</id>
		<title>File:DS TR.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:DS_TR.jpg&amp;diff=70283"/>
		<updated>2012-11-18T19:19:31Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: Image showing &amp;quot;Debugging Sucks, Testing Rocks&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Image showing &amp;quot;Debugging Sucks, Testing Rocks&amp;quot;&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70282</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w28 dh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70282"/>
		<updated>2012-11-18T19:18:00Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''SaaS - 4.8 - 4.9 - Fallacies &amp;amp; pitfalls, BDD pros &amp;amp; cons'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to Software Engineering Fallacies and Pitfalls==&lt;br /&gt;
&lt;br /&gt;
In the history of the software field, a lot of facts have been identified, and a lot of fallacies promulgated. There’s a problem with those facts–and, as you might imagine, those fallacies. Many of these fundamentally important facts are learned by a software engineer, but over the lifespan of the software field, all too many of them have been forgotten. These facts and fallacies are fundamental to the software building field–forget or neglect them at your peril!&lt;br /&gt;
&lt;br /&gt;
==Fallacies==&lt;br /&gt;
&lt;br /&gt;
Five Fatal Logical Fallacies of Software Development&lt;br /&gt;
&lt;br /&gt;
===Fallacy Fallacy===&lt;br /&gt;
&lt;br /&gt;
Drawing the conclusion that another engineer's point is incorrect because he cannot adequately defend his position. &lt;br /&gt;
Engineers often have an intuition about engineering solutions — an intuition that isn't always easily quantifiable into a well formed argument. Engineer should seek first to understand the possible reasons for taking an opposing position so that, ultimately, his/her designs will be better informed. Moreover, if that engineer is in a decision making position, his/her decisions will be viewed as more fair and well thought out. &lt;br /&gt;
&lt;br /&gt;
===Appeal to Tradition===&lt;br /&gt;
&lt;br /&gt;
Justifying or defending a coding practice by citing precedent.&lt;br /&gt;
It is usually an internal struggle between introducing newer, more well thought out designs vs adopting a 'When in Rome do as the Romans do' strategy to maintain consistency in the code base.&lt;br /&gt;
Processes, procedures and coding practices aren't developed in a vacuum. There are a dizzying array of external factors that influence these decisions. Many times, if one examines the factors that led to these decisions, he/she will find that the original reasons no longer apply or are diminished. It never hurts to apply the five whys to find out if it makes sense to continue a particular practice.&lt;br /&gt;
&lt;br /&gt;
===The Sunk Cost Fallacy===&lt;br /&gt;
&lt;br /&gt;
Believing to continue forward with a particular design, project, etc. because too much time, money and other resources have already been poured into a solution.&lt;br /&gt;
Software engineering is hard. When a project is started,  all of the facts are not present. There should not be any fear to change direction once new information leads to question the viability of the original decisions.&lt;br /&gt;
Is that refactoring effort that you thought would take 2 days instead proving to drag into 2 weeks with no end in sight? Don't be afraid to stop because now it is known that its more involved that it was originally thought. Cut your losses. It will make the the project deadline and the stakeholders happy.&lt;br /&gt;
&lt;br /&gt;
===The False Dichotomy===&lt;br /&gt;
&lt;br /&gt;
Constraining a problem space to two — and only two solutions.&lt;br /&gt;
Statements like the following are often made: 'The slow performance is due to either a missing database index or a changed execution plan from crossing a partition'.&lt;br /&gt;
While these may be likely culprits, such statements ignore an entire universe of other possibilities including JVM GC pauses, network latencies, etc. Rarely are there only two possibilities when dealing with undiagnosed problems.&lt;br /&gt;
&lt;br /&gt;
===Confusing Correlation and Causation===&lt;br /&gt;
&lt;br /&gt;
Assuming that because events occur together that one must cause the other.&lt;br /&gt;
This is another fallacy that tends to be seen when troubleshooting production issues. In such situations, reams of data are analyzed and we humans do what we're best at: pattern recognition. If we're not careful, this pattern recognition can lead to assume that one event causes another to occur.&lt;br /&gt;
Incorrectly asserting causation is dangerous in this case because it can lead down to unproductive exploratory paths and thus lead to longer diagnosis times. Knowing in advance that events are correlated but not necessarily causal can introduce a healthy dose of skepticism into the troubleshooting process.&lt;br /&gt;
&lt;br /&gt;
==Pitfalls==&lt;br /&gt;
&lt;br /&gt;
===Temptation to Add Cool Features===&lt;br /&gt;
&lt;br /&gt;
Adding cool features that do not make the product more successful. Programmers are usually tempted to add cool features that customers don’t like. It is called pitfall because it happens even when programmer knows it. Programmer knows it will be rejected but he/she does it anyways and thinks that the customer will like it. But the customers hate it and it gets rejected.&lt;br /&gt;
The philosophy is use a method such as user stories to help prioritize the tasks that are needed to be done first. Then if there is time for cool features, they can be added. It helps in reducing wasted effort on the features that are not at all required by the customers.&lt;br /&gt;
&lt;br /&gt;
===Predicting Code===&lt;br /&gt;
&lt;br /&gt;
Trying to predict what code you need before need it&lt;br /&gt;
Programmers sometimes love to predict what customer wants and go ahead and put those features in the code. But when programmers show the feature to the customer, they disapprove it.&lt;br /&gt;
Hence there is a need to write the test first, sync with the customer and then implement it. Programmers can use Behavior-Driven-Design (BDD) to write tests before code they need, then write code to pass the tests. In this case there is no need to predict and no wastage of development time and resources.&lt;br /&gt;
&lt;br /&gt;
===Negative Expectations===&lt;br /&gt;
&lt;br /&gt;
Careless use of negative expectations&lt;br /&gt;
While writing tests first, the more detailed thing is when programmers say what is not supposed to happen. But the downside is there are millions of things that are not supposed to happen, so it’s pretty easy to pass the things that are not happening. But one probably do not want to do that. &lt;br /&gt;
Many, many outputs are incorrect. The programmers should be beware of overusing “Then I should not see...”. They cannot tell if output is what they want, by saying only that it is not what they want. Hence it is better to include positives to check results such as “Then I should see...”. It will clearly state what is expected by the program.&lt;br /&gt;
&lt;br /&gt;
===Why Do Softwares Fail===&lt;br /&gt;
&lt;br /&gt;
There is a sad line about software before the agile came into software development field that ‘If we built all the building like we build software, the first woodpecker would destroy civilization.’ So what was the reason behind this saying? Following are the problems with the method of software development before agile and BDD:&lt;br /&gt;
&lt;br /&gt;
===Softwares don’t do what Customers want===&lt;br /&gt;
&lt;br /&gt;
After a software is built and presented to the customer, they realize that “That’s not what they want, even though it sounded like it.” &lt;br /&gt;
&lt;br /&gt;
===Projects are Late===&lt;br /&gt;
&lt;br /&gt;
The problem while making a software project is the estimation of the time to build it. It is very common for the software engineering projects to run out of time. Hence they are delivered late than the date estimated.&lt;br /&gt;
&lt;br /&gt;
===Over Budget===&lt;br /&gt;
&lt;br /&gt;
Another common problem with the software development project is the estimation of the budget. Most of the projects run out of money before they are completed.&lt;br /&gt;
&lt;br /&gt;
===Hard to Maintain and Evolve===&lt;br /&gt;
&lt;br /&gt;
People expect software today to be maintainable or evolve whenever necessary. Sometimes the software project is built perfectly fine for the first time and works as intended. But it is really hard to maintain and evolve later.&lt;br /&gt;
&lt;br /&gt;
This inspired the Agile Manifesto. The solution that Agile methodology provides is to work closely and continuously with the customer, not like sign a contract and come back in two years and show them the results. Not only the customer but all the stakeholders, the end users of the system, developers, people maintaining the code should be involved, so that all are on the same page. Behavior-Driven Design is the design method that gives an idea to capture the behavior that is required as opposed to how it is implemented.&lt;br /&gt;
&lt;br /&gt;
==Behaviour-Driven Design (BDD)==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
Behavior-driven design (BDD) is a software development process which combines the principles of test-driven development and object-oriented analysis and design to provide business analysts and software developers with a process to develop quality software. &lt;br /&gt;
&lt;br /&gt;
A specialized version of Test-driven development which focuses on the behavior of the application being developed is the core of Behavior Driven Development, in which software developers must define a test set for each software unit, make sure the tests fail before implementation of the unit, implement the unit and verify that tests succeed once the unit has been implemented. &lt;br /&gt;
&lt;br /&gt;
BDD asks questions about the behavior of the application being developed before and during development to reduce miscommunication. In this process the requirements are written down as user stories in the form of “3x5” cards, and all the stakeholders can mention their requirements without hesitation. These user stories are used to create acceptance tests before the actual code is written.&lt;br /&gt;
&lt;br /&gt;
[[File:BDD_life_cycle.jpg|thumb|right|alt= Behavior-Driven Development Life Cycle]]&lt;br /&gt;
&lt;br /&gt;
===Pros of using BDD===&lt;br /&gt;
&lt;br /&gt;
The following are some of the advantages of using Behavior-Driven Design:&lt;br /&gt;
&lt;br /&gt;
* Makes it Easy to Work with Customers.&lt;br /&gt;
&lt;br /&gt;
BDD uses user stories which are non-technical and allows all stakeholders to easily understand and state the requirements. Storyboards and UI sketches are used in BDD to allow all stakeholders including nontechnical to contribute towards requirement specification. Using “3x5” user story cards customers can state their requirements more effectively and without the trouble of using technical terminology. &lt;br /&gt;
&lt;br /&gt;
* Enables customer to drive the end product development&lt;br /&gt;
&lt;br /&gt;
Customers are continuously involved in the development of the software so that they can drive (and effectively change) the behaviour of the end product during the course of the development.&lt;br /&gt;
&lt;br /&gt;
* Tests are written before debugging&lt;br /&gt;
&lt;br /&gt;
BDD helps software developers to come up with tests before debugging issues with the software and even before writing the code. This allows the developers to follow the Test-driven software development methodology.&lt;br /&gt;
&lt;br /&gt;
* User stories can be formulated as acceptance tests before code is written&lt;br /&gt;
&lt;br /&gt;
The user stories written by various stakeholders can be directly used to formulate acceptance test cases. So developers have acceptance test cases available even before the code development starts. This can be achieved through the tools such as cucumber in the Rails development framework which takes user stories as input to create acceptance test cases.&lt;br /&gt;
&lt;br /&gt;
* Easy to change requirements during development process&lt;br /&gt;
&lt;br /&gt;
The customer requirements and expectations may evolve and change during the course of software development. BDD has the capability to incorporate new insights from customers during development. This mainly makes use of “3x5” index cards, since the requirements written on these cards are short it is easy to change them during development.&lt;br /&gt;
&lt;br /&gt;
* Customer can keep track of the progress in development through iterations&lt;br /&gt;
&lt;br /&gt;
Software developers get to meet with respective stakeholders once a week and show them how various iterations of the product being developed work.&lt;br /&gt;
&lt;br /&gt;
===Cons of using BDD===&lt;br /&gt;
&lt;br /&gt;
* Customer may not be interested to make continuous contact with developers.&lt;br /&gt;
&lt;br /&gt;
Behavior-driven software development may not be very effective if the customer refuses to make continuous contact with the developer and review the various iterations. The success of BDD depends on continuous interaction with customer so as to shape the end product as per their expectations, therefore it can be disadvantageous to the project if the customer is reluctant to make continuous interactions during product development.&lt;br /&gt;
&lt;br /&gt;
* Leads to bad software architecture&lt;br /&gt;
&lt;br /&gt;
Customer driven software development can sometimes lead to bad software architecture because of continuously evolving customer requirements which then reflects in the software architecture. Software which lacks good architecture makes it hard for developers to maintain the software and add new features to the existing version.&lt;br /&gt;
&lt;br /&gt;
==“Debugging Sucks Testing Rocks”==&lt;br /&gt;
&lt;br /&gt;
This is a philosophy which suggests that tests for softwares should be written in advance so that the developer does not spend all his time debugging various issues in the software. According to this philosophy it is better to invest more time into testing than into debugging, as this approach in turn considerably reduces the time spent on debugging issues.&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70258</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w28 dh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70258"/>
		<updated>2012-11-18T18:49:10Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''SaaS - 4.8 - 4.9 - Fallacies &amp;amp; pitfalls, BDD pros &amp;amp; cons'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to Software Engineering Fallacies and Pitfalls==&lt;br /&gt;
&lt;br /&gt;
In the history of the software field, a lot of facts have been identified, and a lot of fallacies promulgated. There’s a problem with those facts–and, as you might imagine, those fallacies. Many of these fundamentally important facts are learned by a software engineer, but over the lifespan of the software field, all too many of them have been forgotten. These facts and fallacies are fundamental to the software building field–forget or neglect them at your peril!&lt;br /&gt;
&lt;br /&gt;
==Fallacies==&lt;br /&gt;
&lt;br /&gt;
Five Fatal Logical Fallacies of Software Development&lt;br /&gt;
&lt;br /&gt;
===Fallacy Fallacy===&lt;br /&gt;
&lt;br /&gt;
Drawing the conclusion that another engineer's point is incorrect because he cannot adequately defend his position. &lt;br /&gt;
Engineers often have an intuition about engineering solutions — an intuition that isn't always easily quantifiable into a well formed argument. Engineer should seek first to understand the possible reasons for taking an opposing position so that, ultimately, his/her designs will be better informed. Moreover, if that engineer is in a decision making position, his/her decisions will be viewed as more fair and well thought out. &lt;br /&gt;
&lt;br /&gt;
===Appeal to Tradition===&lt;br /&gt;
&lt;br /&gt;
Justifying or defending a coding practice by citing precedent.&lt;br /&gt;
It is usually an internal struggle between introducing newer, more well thought out designs vs adopting a 'When in Rome do as the Romans do' strategy to maintain consistency in the code base.&lt;br /&gt;
Processes, procedures and coding practices aren't developed in a vacuum. There are a dizzying array of external factors that influence these decisions. Many times, if one examines the factors that led to these decisions, he/she will find that the original reasons no longer apply or are diminished. It never hurts to apply the five whys to find out if it makes sense to continue a particular practice.&lt;br /&gt;
&lt;br /&gt;
===The Sunk Cost Fallacy===&lt;br /&gt;
&lt;br /&gt;
Believing to continue forward with a particular design, project, etc. because too much time, money and other resources have already been poured into a solution.&lt;br /&gt;
Software engineering is hard. When a project is started,  all of the facts are not present. There should not be any fear to change direction once new information leads to question the viability of the original decisions.&lt;br /&gt;
Is that refactoring effort that you thought would take 2 days instead proving to drag into 2 weeks with no end in sight? Don't be afraid to stop because now it is known that its more involved that it was originally thought. Cut your losses. It will make the the project deadline and the stakeholders happy.&lt;br /&gt;
&lt;br /&gt;
===The False Dichotomy===&lt;br /&gt;
&lt;br /&gt;
Constraining a problem space to two — and only two solutions.&lt;br /&gt;
Statements like the following are often made: 'The slow performance is due to either a missing database index or a changed execution plan from crossing a partition'.&lt;br /&gt;
While these may be likely culprits, such statements ignore an entire universe of other possibilities including JVM GC pauses, network latencies, etc. Rarely are there only two possibilities when dealing with undiagnosed problems.&lt;br /&gt;
&lt;br /&gt;
===Confusing Correlation and Causation===&lt;br /&gt;
&lt;br /&gt;
Assuming that because events occur together that one must cause the other.&lt;br /&gt;
This is another fallacy that tends to be seen when troubleshooting production issues. In such situations, reams of data are analyzed and we humans do what we're best at: pattern recognition. If we're not careful, this pattern recognition can lead to assume that one event causes another to occur.&lt;br /&gt;
Incorrectly asserting causation is dangerous in this case because it can lead down to unproductive exploratory paths and thus lead to longer diagnosis times. Knowing in advance that events are correlated but not necessarily causal can introduce a healthy dose of skepticism into the troubleshooting process.&lt;br /&gt;
&lt;br /&gt;
==Pitfalls==&lt;br /&gt;
&lt;br /&gt;
===Temptation to Add Cool Features===&lt;br /&gt;
&lt;br /&gt;
Adding cool features that do not make the product more successful. Programmers are usually tempted to add cool features that customers don’t like. It is called pitfall because it happens even when programmer knows it. Programmer knows it will be rejected but he/she does it anyways and thinks that the customer will like it. But the customers hate it and it gets rejected.&lt;br /&gt;
The philosophy is use a method such as user stories to help prioritize the tasks that are needed to be done first. Then if there is time for cool features, they can be added. It helps in reducing wasted effort on the features that are not at all required by the customers.&lt;br /&gt;
&lt;br /&gt;
===Predicting Code===&lt;br /&gt;
&lt;br /&gt;
Trying to predict what code you need before need it&lt;br /&gt;
Programmers sometimes love to predict what customer wants and go ahead and put those features in the code. But when programmers show the feature to the customer, they disapprove it.&lt;br /&gt;
Hence there is a need to write the test first, sync with the customer and then implement it. Programmers can use Behavior-Driven-Design (BDD) to write tests before code they need, then write code to pass the tests. In this case there is no need to predict and no wastage of development time and resources.&lt;br /&gt;
&lt;br /&gt;
===Negative Expectations===&lt;br /&gt;
&lt;br /&gt;
Careless use of negative expectations&lt;br /&gt;
While writing tests first, the more detailed thing is when programmers say what is not supposed to happen. But the downside is there are millions of things that are not supposed to happen, so it’s pretty easy to pass the things that are not happening. But one probably do not want to do that. &lt;br /&gt;
Many, many outputs are incorrect. The programmers should be beware of overusing “Then I should not see...”. They cannot tell if output is what they want, by saying only that it is not what they want. Hence it is better to include positives to check results such as “Then I should see...”. It will clearly state what is expected by the program.&lt;br /&gt;
&lt;br /&gt;
===Why Do Softwares Fail===&lt;br /&gt;
&lt;br /&gt;
There is a sad line about software before the agile came into software development field that ‘If we built all the building like we build software, the first woodpecker would destroy civilization.’ So what was the reason behind this saying? Following are the problems with the method of software development before agile and BDD:&lt;br /&gt;
&lt;br /&gt;
===Softwares don’t do what Customers want===&lt;br /&gt;
&lt;br /&gt;
After a software is built and presented to the customer, they realize that “That’s not what they want, even though it sounded like it.” &lt;br /&gt;
&lt;br /&gt;
===Projects are Late===&lt;br /&gt;
&lt;br /&gt;
The problem while making a software project is the estimation of the time to build it. It is very common for the software engineering projects to run out of time. Hence they are delivered late than the date estimated.&lt;br /&gt;
&lt;br /&gt;
===Over Budget===&lt;br /&gt;
&lt;br /&gt;
Another common problem with the software development project is the estimation of the budget. Most of the projects run out of money before they are completed.&lt;br /&gt;
&lt;br /&gt;
===Hard to Maintain and Evolve===&lt;br /&gt;
&lt;br /&gt;
People expect software today to be maintainable or evolve whenever necessary. Sometimes the software project is built perfectly fine for the first time and works as intended. But it is really hard to maintain and evolve later.&lt;br /&gt;
&lt;br /&gt;
This inspired the Agile Manifesto. The solution that Agile methodology provides is to work closely and continuously with the customer, not like sign a contract and come back in two years and show them the results. Not only the customer but all the stakeholders, the end users of the system, developers, people maintaining the code should be involved, so that all are on the same page. Behavior-Driven Design is the design method that gives an idea to capture the behavior that is required as opposed to how it is implemented.&lt;br /&gt;
&lt;br /&gt;
==Behaviour-Driven Design (BDD)==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
Behavior-driven design (BDD) is a software development process which combines the principles of test-driven development and object-oriented analysis and design to provide business analysts and software developers with a process to develop quality software. &lt;br /&gt;
&lt;br /&gt;
A specialized version of Test-driven development which focuses on the behavior of the application being developed is the core of Behavior Driven Development, in which software developers must define a test set for each software unit, make sure the tests fail before implementation of the unit, implement the unit and verify that tests succeed once the unit has been implemented. &lt;br /&gt;
&lt;br /&gt;
BDD asks questions about the behavior of the application being developed before and during development to reduce miscommunication. In this process the requirements are written down as user stories in the form of “3x5” cards, and all the stakeholders can mention their requirements without hesitation. These user stories are used to create acceptance tests before the actual code is written.&lt;br /&gt;
&lt;br /&gt;
[[File:BDD_life_cycle.jpg|thumb|right|alt= Behavior-Driven Development Life Cycle]]&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:BDD_life_cycle.jpg&amp;diff=70257</id>
		<title>File:BDD life cycle.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:BDD_life_cycle.jpg&amp;diff=70257"/>
		<updated>2012-11-18T18:45:23Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: Describes Behavior-Driven Development Life Cycle.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Describes Behavior-Driven Development Life Cycle.&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70256</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w28 dh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70256"/>
		<updated>2012-11-18T18:43:58Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''SaaS - 4.8 - 4.9 - Fallacies &amp;amp; pitfalls, BDD pros &amp;amp; cons'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to Software Engineering Fallacies and Pitfalls==&lt;br /&gt;
&lt;br /&gt;
In the history of the software field, a lot of facts have been identified, and a lot of fallacies promulgated. There’s a problem with those facts–and, as you might imagine, those fallacies. Many of these fundamentally important facts are learned by a software engineer, but over the lifespan of the software field, all too many of them have been forgotten. These facts and fallacies are fundamental to the software building field–forget or neglect them at your peril!&lt;br /&gt;
&lt;br /&gt;
==Fallacies==&lt;br /&gt;
&lt;br /&gt;
Five Fatal Logical Fallacies of Software Development&lt;br /&gt;
&lt;br /&gt;
===Fallacy Fallacy===&lt;br /&gt;
&lt;br /&gt;
Drawing the conclusion that another engineer's point is incorrect because he cannot adequately defend his position. &lt;br /&gt;
Engineers often have an intuition about engineering solutions — an intuition that isn't always easily quantifiable into a well formed argument. Engineer should seek first to understand the possible reasons for taking an opposing position so that, ultimately, his/her designs will be better informed. Moreover, if that engineer is in a decision making position, his/her decisions will be viewed as more fair and well thought out. &lt;br /&gt;
&lt;br /&gt;
===Appeal to Tradition===&lt;br /&gt;
&lt;br /&gt;
Justifying or defending a coding practice by citing precedent.&lt;br /&gt;
It is usually an internal struggle between introducing newer, more well thought out designs vs adopting a 'When in Rome do as the Romans do' strategy to maintain consistency in the code base.&lt;br /&gt;
Processes, procedures and coding practices aren't developed in a vacuum. There are a dizzying array of external factors that influence these decisions. Many times, if one examines the factors that led to these decisions, he/she will find that the original reasons no longer apply or are diminished. It never hurts to apply the five whys to find out if it makes sense to continue a particular practice.&lt;br /&gt;
&lt;br /&gt;
===The Sunk Cost Fallacy===&lt;br /&gt;
&lt;br /&gt;
Believing to continue forward with a particular design, project, etc. because too much time, money and other resources have already been poured into a solution.&lt;br /&gt;
Software engineering is hard. When a project is started,  all of the facts are not present. There should not be any fear to change direction once new information leads to question the viability of the original decisions.&lt;br /&gt;
Is that refactoring effort that you thought would take 2 days instead proving to drag into 2 weeks with no end in sight? Don't be afraid to stop because now it is known that its more involved that it was originally thought. Cut your losses. It will make the the project deadline and the stakeholders happy.&lt;br /&gt;
&lt;br /&gt;
===The False Dichotomy===&lt;br /&gt;
&lt;br /&gt;
Constraining a problem space to two — and only two solutions.&lt;br /&gt;
Statements like the following are often made: 'The slow performance is due to either a missing database index or a changed execution plan from crossing a partition'.&lt;br /&gt;
While these may be likely culprits, such statements ignore an entire universe of other possibilities including JVM GC pauses, network latencies, etc. Rarely are there only two possibilities when dealing with undiagnosed problems.&lt;br /&gt;
&lt;br /&gt;
===Confusing Correlation and Causation===&lt;br /&gt;
&lt;br /&gt;
Assuming that because events occur together that one must cause the other.&lt;br /&gt;
This is another fallacy that tends to be seen when troubleshooting production issues. In such situations, reams of data are analyzed and we humans do what we're best at: pattern recognition. If we're not careful, this pattern recognition can lead to assume that one event causes another to occur.&lt;br /&gt;
Incorrectly asserting causation is dangerous in this case because it can lead down to unproductive exploratory paths and thus lead to longer diagnosis times. Knowing in advance that events are correlated but not necessarily causal can introduce a healthy dose of skepticism into the troubleshooting process.&lt;br /&gt;
&lt;br /&gt;
==Pitfalls==&lt;br /&gt;
&lt;br /&gt;
===Temptation to Add Cool Features===&lt;br /&gt;
&lt;br /&gt;
Adding cool features that do not make the product more successful. Programmers are usually tempted to add cool features that customers don’t like. It is called pitfall because it happens even when programmer knows it. Programmer knows it will be rejected but he/she does it anyways and thinks that the customer will like it. But the customers hate it and it gets rejected.&lt;br /&gt;
The philosophy is use a method such as user stories to help prioritize the tasks that are needed to be done first. Then if there is time for cool features, they can be added. It helps in reducing wasted effort on the features that are not at all required by the customers.&lt;br /&gt;
&lt;br /&gt;
===Predicting Code===&lt;br /&gt;
&lt;br /&gt;
Trying to predict what code you need before need it&lt;br /&gt;
Programmers sometimes love to predict what customer wants and go ahead and put those features in the code. But when programmers show the feature to the customer, they disapprove it.&lt;br /&gt;
Hence there is a need to write the test first, sync with the customer and then implement it. Programmers can use Behavior-Driven-Design (BDD) to write tests before code they need, then write code to pass the tests. In this case there is no need to predict and no wastage of development time and resources.&lt;br /&gt;
&lt;br /&gt;
===Negative Expectations===&lt;br /&gt;
&lt;br /&gt;
Careless use of negative expectations&lt;br /&gt;
While writing tests first, the more detailed thing is when programmers say what is not supposed to happen. But the downside is there are millions of things that are not supposed to happen, so it’s pretty easy to pass the things that are not happening. But one probably do not want to do that. &lt;br /&gt;
Many, many outputs are incorrect. The programmers should be beware of overusing “Then I should not see...”. They cannot tell if output is what they want, by saying only that it is not what they want. Hence it is better to include positives to check results such as “Then I should see...”. It will clearly state what is expected by the program.&lt;br /&gt;
&lt;br /&gt;
===Why Do Softwares Fail===&lt;br /&gt;
&lt;br /&gt;
There is a sad line about software before the agile came into software development field that ‘If we built all the building like we build software, the first woodpecker would destroy civilization.’ So what was the reason behind this saying? Following are the problems with the method of software development before agile and BDD:&lt;br /&gt;
&lt;br /&gt;
===Softwares don’t do what Customers want===&lt;br /&gt;
&lt;br /&gt;
After a software is built and presented to the customer, they realize that “That’s not what they want, even though it sounded like it.” &lt;br /&gt;
&lt;br /&gt;
===Projects are Late===&lt;br /&gt;
&lt;br /&gt;
The problem while making a software project is the estimation of the time to build it. It is very common for the software engineering projects to run out of time. Hence they are delivered late than the date estimated.&lt;br /&gt;
&lt;br /&gt;
===Over Budget===&lt;br /&gt;
&lt;br /&gt;
Another common problem with the software development project is the estimation of the budget. Most of the projects run out of money before they are completed.&lt;br /&gt;
&lt;br /&gt;
===Hard to Maintain and Evolve===&lt;br /&gt;
&lt;br /&gt;
People expect software today to be maintainable or evolve whenever necessary. Sometimes the software project is built perfectly fine for the first time and works as intended. But it is really hard to maintain and evolve later.&lt;br /&gt;
&lt;br /&gt;
This inspired the Agile Manifesto. The solution that Agile methodology provides is to work closely and continuously with the customer, not like sign a contract and come back in two years and show them the results. Not only the customer but all the stakeholders, the end users of the system, developers, people maintaining the code should be involved, so that all are on the same page. Behavior-Driven Design is the design method that gives an idea to capture the behavior that is required as opposed to how it is implemented.&lt;br /&gt;
&lt;br /&gt;
==Behaviour-Driven Design (BDD)==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
Behavior-driven design (BDD) is a software development process which combines the principles of test-driven development and object-oriented analysis and design to provide business analysts and software developers with a process to develop quality software. &lt;br /&gt;
&lt;br /&gt;
A specialized version of Test-driven development which focuses on the behavior of the application being developed is the core of Behavior Driven Development, in which software developers must define a test set for each software unit, make sure the tests fail before implementation of the unit, implement the unit and verify that tests succeed once the unit has been implemented. &lt;br /&gt;
&lt;br /&gt;
BDD asks questions about the behavior of the application being developed before and during development to reduce miscommunication. In this process the requirements are written down as user stories in the form of “3x5” cards, and all the stakeholders can mention their requirements without hesitation. These user stories are used to create acceptance tests before the actual code is written.&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70241</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w28 dh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w28_dh&amp;diff=70241"/>
		<updated>2012-11-18T18:02:22Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: Created page with &amp;quot;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''SaaS - 4.8 - 4.9 - Fallacies &amp;amp; pitfalls, BDD pros &amp;amp; cons'''   ==Introduction to Software Engineering Fallacies and Pitfalls==  In the history of the...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''SaaS - 4.8 - 4.9 - Fallacies &amp;amp; pitfalls, BDD pros &amp;amp; cons'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction to Software Engineering Fallacies and Pitfalls==&lt;br /&gt;
&lt;br /&gt;
In the history of the software field, a lot of facts have been identified, and a lot of fallacies promulgated. There’s a problem with those facts–and, as you might imagine, those fallacies. Many of these fundamentally important facts are learned by a software engineer, but over the lifespan of the software field, all too many of them have been forgotten. These facts and fallacies are fundamental to the software building field–forget or neglect them at your peril!&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70215</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70215"/>
		<updated>2012-11-18T17:45:10Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/Table_Of_Contents]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w58 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w60 ac]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w62 rb]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w29 st]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w3_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w30 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w17 pt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w31 up]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w9 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w19 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w26 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w16 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w8 vp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w3 jm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w23 sr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w11_aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w15 rr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w33 pv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w20_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w14_bb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w21_ap]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w13_sm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w4_sa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w25_nr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w12_sv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w7_ma]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w6_ar]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w32_mk]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w10_rc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w70_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w67_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w40_sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w22_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w-1w65_am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w59_bc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w60_ns]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b 2w47 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w69_as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w39_ka]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w36_av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w37_ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w43_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w53_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w63_sp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w49_ps]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w52_sj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w28_dh]]&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=67299</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=67299"/>
		<updated>2012-10-08T03:07:12Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a [http://en.wikipedia.org/wiki/Software_design_pattern Design pattern] in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realized through [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use [http://en.wikipedia.org/wiki/SQL SQL] in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like [http://en.wikipedia.org/wiki/MySQL MySQL], [http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] and [http://en.wikipedia.org/wiki/SQLite SQLite]). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on files, classes, tables etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
The Classes inheriting from ActiveRecord are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by most calls in the standard ActiveRecord [http://en.wikipedia.org/wiki/Application_programming_interface API]. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the [http://en.wikipedia.org/wiki/Timestamp timestamp]. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a [http://en.wikipedia.org/wiki/Unique_key primary key] of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model by going ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model by proceeding ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the tables in the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
&lt;br /&gt;
*[http://www.youtube.com/watch?v=PFT1KTL0ijk Rails ActiveRecord Basic Associations]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord/Connection_adapters Connection Adapters]&lt;br /&gt;
*[http://rubylearning.com/satishtalim/ruby_activerecord_and_mysql.html ActiveRecord and MySQL]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://guides.rubyonrails.org Rails Guides]&lt;br /&gt;
#[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord ActiveRecord - The Model]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/ActiveRecord_(Rails)#Ruby ActiveRecord Pattern - Wikipedia]&lt;br /&gt;
#[http://www.developerfusion.com/article/84435/activerecord-for-ruby-and-rails/ Kevin Jones - ActiveRecord for Ruby and Rails]&lt;br /&gt;
#[http://www.daniel-azuma.com/blog/archives/216 Daniel Azuma - Setting the Database with ActiveRecord’s Connection API]&lt;br /&gt;
#Agile Web Development with Rails - Sam Ruby, Dave Thomas, David Hansson, 3rd Edition.&lt;br /&gt;
#Ruby on Rails Bible - Timothy Fisher&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=67298</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=67298"/>
		<updated>2012-10-08T02:55:43Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a [http://en.wikipedia.org/wiki/Software_design_pattern Design pattern] in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realized through [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use [http://en.wikipedia.org/wiki/SQL SQL] in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like [http://en.wikipedia.org/wiki/MySQL MySQL], [http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] and [http://en.wikipedia.org/wiki/SQLite SQLite]). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on files, classes, tables etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
The Classes inheriting from ActiveRecord are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by most calls in the standard ActiveRecord [http://en.wikipedia.org/wiki/Application_programming_interface API]. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the [http://en.wikipedia.org/wiki/Timestamp timestamp]. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a [http://en.wikipedia.org/wiki/Unique_key primary key] of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model by going ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model by proceeding ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the tables in the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://guides.rubyonrails.org Rails Guides]&lt;br /&gt;
#[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord ActiveRecord - The Model]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/ActiveRecord_(Rails)#Ruby ActiveRecord Pattern - Wikipedia]&lt;br /&gt;
#[http://www.developerfusion.com/article/84435/activerecord-for-ruby-and-rails/ Kevin Jones - ActiveRecord for Ruby and Rails]&lt;br /&gt;
#[http://www.daniel-azuma.com/blog/archives/216 Daniel Azuma - Setting the Database with ActiveRecord’s Connection API]&lt;br /&gt;
#Agile Web Development with Rails - Sam Ruby, Dave Thomas, David Hansson, 3rd Edition.&lt;br /&gt;
#Ruby on Rails Bible - Timothy Fisher&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=67293</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=67293"/>
		<updated>2012-10-07T18:46:39Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on files, classes, tables etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
The Classes inheriting from ActiveRecord are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by most calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model by going ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model by proceeding ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the tables in the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://guides.rubyonrails.org Rails Guides]&lt;br /&gt;
#[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord ActiveRecord - The Model]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/ActiveRecord_(Rails)#Ruby ActiveRecord Pattern - Wikipedia]&lt;br /&gt;
#[http://www.developerfusion.com/article/84435/activerecord-for-ruby-and-rails/ Kevin Jones - ActiveRecord for Ruby and Rails]&lt;br /&gt;
#[http://www.daniel-azuma.com/blog/archives/216 Daniel Azuma - Setting the Database with ActiveRecord’s Connection API]&lt;br /&gt;
#Agile Web Development with Rails - Sam Ruby, Dave Thomas, David Hansson, 3rd Edition.&lt;br /&gt;
#Ruby on Rails Bible - Timothy Fisher&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66840</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66840"/>
		<updated>2012-10-04T00:22:40Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on files, classes, tables etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
The Classes inheriting from ActiveRecord are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model by going ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model by proceeding ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the tables in the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://guides.rubyonrails.org Rails Guides]&lt;br /&gt;
#[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord ActiveRecord - The Model]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/ActiveRecord_(Rails)#Ruby ActiveRecord Pattern - Wikipedia]&lt;br /&gt;
#[http://www.developerfusion.com/article/84435/activerecord-for-ruby-and-rails/ Kevin Jones - ActiveRecord for Ruby and Rails]&lt;br /&gt;
#[http://www.daniel-azuma.com/blog/archives/216 Daniel Azuma - Setting the Database with ActiveRecord’s Connection API]&lt;br /&gt;
#Agile Web Development with Rails - Sam Ruby, Dave Thomas, David Hansson, 3rd Edition.&lt;br /&gt;
#Ruby on Rails Bible - Timothy Fisher&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66834</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66834"/>
		<updated>2012-10-04T00:21:00Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on files, classes, tables etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
The Classes inheriting from ActiveRecord are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model by going ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model by proceeding ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the tables in the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://guides.rubyonrails.org Rails Guides]&lt;br /&gt;
#[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord ActiveRecord - The Model]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/ActiveRecord_(Rails)#Ruby ActiveRecord Pattern - Wikipedia]&lt;br /&gt;
#[http://www.developerfusion.com/article/84435/activerecord-for-ruby-and-rails/ Kevin Jones - ActiveRecord for Ruby and Rails]&lt;br /&gt;
#[http://www.daniel-azuma.com/blog/archives/216 Daniel Azuma - Setting the Database with ActiveRecord’s Connection API]&lt;br /&gt;
#Agile Web Development with Rails - Sam Ruby, Dave Thomas, David Hansson, 3rd Edition.&lt;br /&gt;
#Ruby on Rails Bible - Timothy Fisher&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66830</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66830"/>
		<updated>2012-10-04T00:17:28Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on files, classes, tables etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
The Classes inheriting from ActiveRecord are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model by going ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model by proceeding ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://guides.rubyonrails.org Rails Guides]&lt;br /&gt;
#[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord ActiveRecord - The Model]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/ActiveRecord_(Rails)#Ruby ActiveRecord Pattern - Wikipedia]&lt;br /&gt;
#[http://www.developerfusion.com/article/84435/activerecord-for-ruby-and-rails/ Kevin Jones - ActiveRecord for Ruby and Rails]&lt;br /&gt;
#[http://www.daniel-azuma.com/blog/archives/216 Daniel Azuma - Setting the Database with ActiveRecord’s Connection API]&lt;br /&gt;
#Agile Web Development with Rails - Sam Ruby, Dave Thomas, David Hansson, 3rd Edition.&lt;br /&gt;
#Ruby on Rails Bible - Timothy Fisher&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66771</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66771"/>
		<updated>2012-10-03T23:51:06Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on file naming, class naming, table naming etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord classes are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model by going ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model by proceeding ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://guides.rubyonrails.org Rails Guides]&lt;br /&gt;
#[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord ActiveRecord - The Model]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/ActiveRecord_(Rails)#Ruby ActiveRecord Pattern - Wikipedia]&lt;br /&gt;
#[http://www.developerfusion.com/article/84435/activerecord-for-ruby-and-rails/ Kevin Jones - ActiveRecord for Ruby and Rails]&lt;br /&gt;
#[http://www.daniel-azuma.com/blog/archives/216 Daniel Azuma - Setting the Database with ActiveRecord’s Connection API]&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66673</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66673"/>
		<updated>2012-10-03T23:21:29Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on file naming, class naming, table naming etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord classes are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://guides.rubyonrails.org Rails Guides]&lt;br /&gt;
#[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord ActiveRecord - The Model]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/ActiveRecord_(Rails)#Ruby ActiveRecord Pattern - Wikipedia]&lt;br /&gt;
#[http://www.developerfusion.com/article/84435/activerecord-for-ruby-and-rails/ Kevin Jones - ActiveRecord for Ruby and Rails]&lt;br /&gt;
#[http://www.daniel-azuma.com/blog/archives/216 Daniel Azuma - Setting the Database with ActiveRecord’s Connection API]&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66663</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66663"/>
		<updated>2012-10-03T23:15:41Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on file naming, class naming, table naming etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord classes are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://guides.rubyonrails.org Rails Guides]&lt;br /&gt;
#[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord ActiveRecord - The Model]&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66629</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66629"/>
		<updated>2012-10-03T22:54:58Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on file naming, class naming, table naming etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord classes are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://guides.rubyonrails.org Rails Guides]&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66577</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66577"/>
		<updated>2012-10-03T22:33:06Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on file naming, class naming, table naming etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord classes are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66560</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66560"/>
		<updated>2012-10-03T22:23:24Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on file naming, class naming, table naming etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord classes are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Create===&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
===Read===&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
===Dynamic Finders===&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
===Update===&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
Thus Active Record allows developers to create the model layer of Web applications with ease and simplicity.&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66557</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66557"/>
		<updated>2012-10-03T22:21:00Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on file naming, class naming, table naming etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord classes are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3.1 Create&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
3.2 Read&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
Dynamic Finders&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
3.3 Update&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3.4 Delete&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for applications using the Rails script/generate script.&lt;br /&gt;
* Connect to database and use Rails migrations to create and modify the database.&lt;br /&gt;
* Search and find records in the database using a number of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows or drop the tables in a database. &lt;br /&gt;
* Model database relations using ActiveRecord methods.&lt;br /&gt;
* Connect various models using associations.&lt;br /&gt;
&lt;br /&gt;
Thus Active Record allows developers to create the model layer of Web applications with ease and simplicity.&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66547</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66547"/>
		<updated>2012-10-03T22:12:15Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on file naming, class naming, table naming etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord classes are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3.1 Create&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
3.2 Read&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
Dynamic Finders&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
3.3 Update&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3.4 Delete&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the time of creation or modification of the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how to go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect two models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to set up a many-to-many association with another model ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to set up a one-to-one connection with another model ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
Active Record can be used to create the model layer for a web application and can be used to do the following tasks:&lt;br /&gt;
&lt;br /&gt;
* Create models for your application using the Rails script/generate script.&lt;br /&gt;
* Use Rails migrations to create and modify your database.&lt;br /&gt;
* Find records in your database using a variety of ActiveRecord methods.&lt;br /&gt;
* Update and delete rows in your database.&lt;br /&gt;
* Model your database relations using ActiveRecord methods.&lt;br /&gt;
* Add data validations to your model classes.&lt;br /&gt;
&lt;br /&gt;
Thus Active Record allows developers to create the model layer of Web applications with ease and simplicity.&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66479</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66479"/>
		<updated>2012-10-03T20:45:52Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on file naming, class naming, table naming etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord classes are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3.1 Create&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
3.2 Read&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
Dynamic Finders&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
3.3 Update&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3.4 Delete&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the times of creation or modification the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how can he go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect 2 models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to setup a many-to-many association with another model ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to setup a one-to-one connection with another model ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_and_belongs_to_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_and_belongs_to_many'' association creates a many-to-many connection with another model without any model in between. In the cookbook example with recipe and category models, if the recipe is allowed to be in more than one category then the ''has_and_belongs_to_many'' association can be used.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_and_belongs_to_many  :recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66473</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w63 dv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w63_dv&amp;diff=66473"/>
		<updated>2012-10-03T20:28:25Z</updated>

		<summary type="html">&lt;p&gt;Dthomas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;'''Active Records'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
The ''Active Record pattern'' is a Design pattern in Software Engineering which deals with the approach to store and access data in a database. The interface of an object conforming to this pattern would contain functions to perform operations like Insert,  Read, Update, and Delete. The Object will have properties that correspond to the columns in the underlying database table. This pattern is realised through ORM (Object-Relational Mapping) libraries in Programming languages.&lt;br /&gt;
&lt;br /&gt;
''ActiveRecord'' is a module for Ruby that can be used for ORM. Thus, it is obvious that ActiveRecord will form a part of the Model in an MVC application developed in Ruby. The rest of the article discusses ActiveRecord that is the Ruby module for implementing the Active Record pattern. &lt;br /&gt;
&lt;br /&gt;
The ActiveRecord module insulates the developer from the need to use SQL in most cases. Internally, It will perform queries on the database which corresponds to the method invoked on the object. This module is compatible with most database systems (most used ones like MySQL, PostgreSQL and SQLite). Moreover, regardless of which database system the developer uses, the Active Record method format always remains the same.&lt;br /&gt;
&lt;br /&gt;
==Naming==&lt;br /&gt;
&lt;br /&gt;
	The ActiveRecord module uses a convention for naming classes, tables and fields so that the amount of configuration needed to get the functionality working is minimal. There are naming conventions on file naming, class naming, table naming etc.&lt;br /&gt;
&lt;br /&gt;
===Reserved names and Attributes===&lt;br /&gt;
    &lt;br /&gt;
Certain names are reserved and should not be used (even in the model as attributes). Some of them are listed below:&lt;br /&gt;
&lt;br /&gt;
*lock_version.&lt;br /&gt;
*type - This is only used when you have single table inheritance and must contain a class name.&lt;br /&gt;
*id - Reserved for primary keys.&lt;br /&gt;
*table_name_count - Reserved for counter cache.&lt;br /&gt;
*position - Reserved for acts_as_list. &lt;br /&gt;
*parent_id - Reserved for acts_as_tree.&lt;br /&gt;
*lft - Reserved for acts_as_nested_set.&lt;br /&gt;
*rgt - Reserved for acts_as_nested_set.&lt;br /&gt;
*quote - Method in ActiveRecord::Base which is used to quote SQL.&lt;br /&gt;
*template.&lt;br /&gt;
&lt;br /&gt;
===Class Naming===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord classes are named in singular form. e.g User&lt;br /&gt;
&lt;br /&gt;
===Table Naming===&lt;br /&gt;
&lt;br /&gt;
Tables for ActiveRecord objects are named in plural form by default. e.g. Users&lt;br /&gt;
This naming convention can be circumvented by using below:&lt;br /&gt;
&lt;br /&gt;
a) Set use_pluralization&lt;br /&gt;
In the environment.rb file we can specify &lt;br /&gt;
   ActiveRecord::Base.use_pluralization = false. &lt;br /&gt;
&lt;br /&gt;
This will disable pluralization for all ActiveRecord objects.&lt;br /&gt;
&lt;br /&gt;
b.) Use set_table_name&lt;br /&gt;
You can call set_table_name to specify a custom table name for a particular model.&lt;br /&gt;
For example:&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    set_table_name 'user'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CRUD ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
CRUD stands for 'Create', 'Read' , 'Update' and 'Delete'. These are the four basic operations which are generally performed on tables in a database. The ActiveRecord module provides predefined methods for the basic CRUD operations for the model.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3.1 Create&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A new record can be created in the table by invoking the “save” function on the model object whose record has to be created in the database. ActiveRecord will use the Object's attributes as the field values for the record. The data is not persisted in the database until we call the save function. &lt;br /&gt;
&lt;br /&gt;
   @user = User.new&lt;br /&gt;
   @user.name = “abc”&lt;br /&gt;
   @user.age = 23&lt;br /&gt;
   @user.save      #returns a boolean indicating whether the save was successful or not (whether a new record was created or not)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ActiveRecord provides another convinient way to create a new record without creating instantiating the model explicitly and then using “save”.   To do this, we use the 'create' function. By default the primary key used in the table is “id” which is generated automatically.&lt;br /&gt;
&lt;br /&gt;
   User.create(:name=&amp;gt;”xyz”, :age=”23”)&lt;br /&gt;
&lt;br /&gt;
3.2 Read&lt;br /&gt;
&lt;br /&gt;
A record can be read from the table by using the various functions like “find” (find the model record by specifying a value used in its primary key),  “where”, “all” , “first” and “last”.  All these functions instantiate a new Object for the model and populate its attributes using the fields of the record.&lt;br /&gt;
&lt;br /&gt;
   @user_first = User.first #Finds and returns the 1st User from the table&lt;br /&gt;
   @user_last = User.last #Finds and returns the Last User from the table&lt;br /&gt;
   @all_users = User.all # Returns all the Users from the table&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_other_user = User.where(:name=&amp;gt;”abc”) #Finds and returns the user whose “name” is “abc” .&lt;br /&gt;
&lt;br /&gt;
Dynamic Finders&lt;br /&gt;
&lt;br /&gt;
Some of the most common searchs performed on databases are to return the rows where a column matches a given value. In many other languages and frameworks, we would generally need to construct SQL queries to perform these searches. ActiveRecord uses Ruby’s dynamic power to do this for us.&lt;br /&gt;
[edit] Connecting to the Database&lt;br /&gt;
For example, our User model has attributes such as name and age. We can use these names in finder methods to return rows where the corresponding columns match some value:&lt;br /&gt;
   @my_user = User.find_by_name(“abc”)&lt;br /&gt;
   @my_user = User.find_by_age(15)&lt;br /&gt;
&lt;br /&gt;
3.3 Update&lt;br /&gt;
&lt;br /&gt;
A record in the table corresponding to a given model instance can be Updated by using the function “save”. &lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.name = “test”&lt;br /&gt;
   @my_user.save&lt;br /&gt;
&lt;br /&gt;
Moreover, we can combine the functions of reading a row and updating it using the class methods update and update_all. The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.&lt;br /&gt;
	&lt;br /&gt;
   @my_user = User.update(1, :name=”test3”)&lt;br /&gt;
   @result = User.update_all(“age= age+1”)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3.4 Delete&lt;br /&gt;
&lt;br /&gt;
A record can be deleted from the table by invoking the “destroy” functionality on the instance of the object.&lt;br /&gt;
The destroy instance method deletes from the database the row corresponding to a particular model object. It then freezes the contents of that object, preventing future changes to the attributes.&lt;br /&gt;
&lt;br /&gt;
   @my_user = User.find(5) #Finds and returns the record from the users table whose id = 5&lt;br /&gt;
   @my_user.destroy # deletes the record corresponding to the user with id = 5 from the table&lt;br /&gt;
   # ... my_user is now frozen&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It also has two class-level methods, delete and delete_all. The delete method takes a single id or an array of ids and deletes the corresponding row(s) in the underlying table. delete_all deletes rows matching a given condition. &lt;br /&gt;
   User.delete(1)&lt;br /&gt;
   User.delete([2,3,4,5])&lt;br /&gt;
   User.delete_all([&amp;quot;age &amp;lt; ?&amp;quot; , 18])&lt;br /&gt;
&lt;br /&gt;
The “delete” methods bypass the ActiveRecord callback and validation functions that may be defined for the model class, while the “destroy” methods ensure that they are all invoked. Hence, it is better to use the “destroy” methods as it ensures that our database is as per the business rules defined in the model.&lt;br /&gt;
&lt;br /&gt;
==Connecting to the Database==&lt;br /&gt;
&lt;br /&gt;
The ActiveRecord connection adapter is meant to wrap and abstract away the underlying driver specific to database, and is meant to provide an interface which is common for database tasks such as creating and destroying databases, modifying tables, updating, deleting, and inserting data, managing transactions and running queries . The connection adapter is normally used internally by ActiveRecord but can be used without the help of ActiveRecord models as well.&lt;br /&gt;
&lt;br /&gt;
A connection adapter can be obtained in the following manner:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  object = Category.find(1)&lt;br /&gt;
  connection = object.connection&lt;br /&gt;
&lt;br /&gt;
Most applications connect to only one database which is defined in the database.yml file. In such a scenario every class which inherits from ActiveRecord::Base will be using the same connection. But in some special cases the application may also connect to a secondary database. That is the case in which some ActiveRecord classes connect to a secondary database. In such cases extra care need to take so that every class asks for a connection from the right database.&lt;br /&gt;
&lt;br /&gt;
Rails generally opens several connections at once, and these connections are managed in a pool. Each connection adapter object forms a single connection to some database. Connections can run only one SQL statement at a time, so generally one connection is opened per thread. When a job needs a connection to database, it checks out one of the pool which is returned when it finishes for use by another task.&lt;br /&gt;
&lt;br /&gt;
===Running Low-Level Queries===&lt;br /&gt;
&lt;br /&gt;
ActiveRecord “model” objects are returned by ost calls in the standard ActiveRecord API. There might be cases in which you want to bypass the overhead involved in creating full ActiveRecord objects, or maybe want to query data that does not have a corresponding ActiveRecord class. SQL queries can be written using the connection adapter's low-level query methods.&lt;br /&gt;
&lt;br /&gt;
In this first example, we get the “category_name” value from a single row in our “categoriess” table. If we only need the category name, we can grab the connection object and use the select_value method as shown below:&lt;br /&gt;
&lt;br /&gt;
  connection = Category.connection&lt;br /&gt;
  category_name = connection.select_value(&amp;quot;SELECT name FROM categories WHERE id=1&amp;quot;)&lt;br /&gt;
  # =&amp;gt; &amp;quot;Football&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Migrations==&lt;br /&gt;
&lt;br /&gt;
Migrations help to version the various changes made to databases. It also allow developers to track a set of changes made to production or development databases and to rollback to a previous version if needed.&lt;br /&gt;
&lt;br /&gt;
=== Building a Migration ===&lt;br /&gt;
You can either build the migration on its own using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate migration User&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specific commands can be written afterwards to create custom SQL. A model can also be created that comes with the migration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;ruby script/generate model User name:string user_id:integer&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The migration will generate a couple of new files under the &amp;quot;db&amp;quot; directory. The contents of such a generate file are as follows:&lt;br /&gt;
&lt;br /&gt;
  # 9889904091223123_create_user.rb&lt;br /&gt;
  class CreateUsers &amp;lt; ActiveRecord::Migration&lt;br /&gt;
    def self.up&lt;br /&gt;
      create_table :users do |t|&lt;br /&gt;
        t.string :name&lt;br /&gt;
        t.integer :user_id&lt;br /&gt;
        t.timestamps&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def self.down&lt;br /&gt;
      drop_table :users&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;9889904091223123&amp;quot; at the beginning of the file-name is the timestamp. The timestamps will be different depending on the times of creation or modification the database. This is helpful to rollback to a previous state if needed. This way the developer need not remember how the current state is reached and how can he go back to a previous state.&lt;br /&gt;
&lt;br /&gt;
The ''self.up'' from the previous code snippet creates the User table and add the columns. The ''self.down'' method is used to drop the table and to remove all the contents. The ''self.up'' and ''self.down'' methods are necessary to keep the database consistent after a rollback.&lt;br /&gt;
&lt;br /&gt;
Rails adds an additional column called the &amp;quot;timestamps&amp;quot; to keep track of when each row was added. Rails also creates a primary key of the form &amp;quot;model_name&amp;quot;_id which increments automatically every time a row is added.&lt;br /&gt;
&lt;br /&gt;
Different types of datatypes can be used with ActiveRecord. Some of the most commonly used ones are:&lt;br /&gt;
&lt;br /&gt;
* integer&lt;br /&gt;
* string&lt;br /&gt;
* text&lt;br /&gt;
* boolean&lt;br /&gt;
* references&lt;br /&gt;
* decimal&lt;br /&gt;
* timestamp&lt;br /&gt;
&lt;br /&gt;
The migration file can be written into the database using the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rake db:migrate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above command will create the table and the various columns. This command can be used to migrate many files in one go. Connections between tables in the database can be introduced using references in the model.&lt;br /&gt;
&lt;br /&gt;
==Associations==&lt;br /&gt;
&lt;br /&gt;
Associations are used to connect 2 models. The association is used to describe the role of relations that models are having with each other. ActiveRecord associations can be used to describe one-to-one (1:1), one-to-many (1:n) and many-to-many (n:m) relationships between models. Associations are used to make common operations simpler and easier in your code. Rails supports six types of associations:&lt;br /&gt;
&lt;br /&gt;
* belongs_to&lt;br /&gt;
* has_one&lt;br /&gt;
* has_many&lt;br /&gt;
* has_many :through&lt;br /&gt;
* has_one :through&lt;br /&gt;
* has_and_belongs_to_many&lt;br /&gt;
&lt;br /&gt;
''belongs_to'' and ''has_one'' form a one-to-one relationship. ''has_one :through'' is a different way to create a one-to-one relationship. ''has_many'' and ''belongs_to'' form a one-to-many relation. ''has_and_belongs_to_many'' or an alternative way ''has_many :through'' to create a many-to-many relationship.&lt;br /&gt;
&lt;br /&gt;
===belongs_to Association===&lt;br /&gt;
&lt;br /&gt;
A ''belongs_to'' association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model. A ''belongs_to'' association can be used to setup a one-to-one or one-to-many relationship with other models. For example, consider a cookbook with recipes and categories such that each recipe &amp;quot;belongs to&amp;quot; a particular category.&lt;br /&gt;
&lt;br /&gt;
  class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :category&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one'' association is used to set up a one-to-one connection with another model such that each instance of a model contains one instance of another model. For example if we have two models User and Account, and each User has a single account, then we can use &amp;quot;has_one&amp;quot; to indicate the relationship between the two models.&lt;br /&gt;
&lt;br /&gt;
  class User &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one: account&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many'' association is used to set up a one-to-may association with other models such that each instance has zero or more instances of another model. In the cookbook example, one category can have many recipes.&lt;br /&gt;
&lt;br /&gt;
  class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many: recipes&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_many :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_many :through'' model is used to setup a many-to-many association with another model ''through'' a third model. In this case, the instance of a model can be connected to many instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. &lt;br /&gt;
&lt;br /&gt;
  class Physician &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :patients, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Appointment &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :physician&lt;br /&gt;
    belongs_to :patient&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Patient &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_many :appointments&lt;br /&gt;
    has_many :physicians, :through =&amp;gt; :appointments&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===has_one :through Association===&lt;br /&gt;
&lt;br /&gt;
A ''has_one :through'' model is used to setup a one-to-one connection with another model ''through'' a third model. In this case, the instance of a model can be connected to one instance of another model through a third model. For example, each client has one account, and each account has one account history.&lt;br /&gt;
&lt;br /&gt;
  class Client &amp;lt; ActiveRecord::Base&lt;br /&gt;
    has_one :account&lt;br /&gt;
    has_one :account_history, :through =&amp;gt; :account&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class Account &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :client&lt;br /&gt;
    has_one :account_history&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  class AccountHistory &amp;lt; ActiveRecord::Base&lt;br /&gt;
    belongs_to :account&lt;br /&gt;
  end&lt;/div&gt;</summary>
		<author><name>Dthomas</name></author>
	</entry>
</feed>